title: Mmap and valgrind, mmap doesnt increase heap size
body: <p>I'm attending operating systems course on my university, one of the tasks we were given is to implement simple malloc using mmap. Now that i got it working i tried to use valgrind to detect any bugs left. And regarldess of freeing memory or not, valgrind doesnt see any memory leaks. As an example consider following C code:</p>
  
  <pre><code>int main()
  {
      int psize = getpagesize(),i;
      int *ptr = mmap(NULL, psize, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
      for(i = 0; i &lt; psize/4; i++) ptr[i] = i;
      for(i = 0; i &lt; psize/4; i++) printf("%d\n", ptr[i]);
      return 0;
  }
  </code></pre>
  
  <p>lets compile it with gcc, and use valgrind. Here is what valgrind returns:</p>
  
  <pre><code>==17841== Memcheck, a memory error detector
  ==17841== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
  ==17841== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
  ==17841== Command: ./test
  ==17841== 
  ------------ printing numbers from 0 to 1023
  ==17841== 
  ==17841== HEAP SUMMARY:
  ==17841==     in use at exit: 0 bytes in 0 blocks
  ==17841==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
  ==17841== 
  ==17841== All heap blocks were freed -- no leaks are possible
  ==17841== 
  ==17841== For counts of detected and suppressed errors, rerun with: -v
  ==17841== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
  </code></pre>
  
  <p>This is something unexpected, we'd normally want pages to be unmapped before exitting program to see such message.</p>
  
  <p>At first i thought that pages might be mapped lazily, this is why i forced performing doing some stuff on that page like changing values, and printing them, but as we can see this is not issue.</p>
  
  <p>This is probably something wrong with valgrind or my understanding of how mmap and valgrind works.</p>