title: mmap problem, allocates huge amounts of memory
body: <p>I got some huge files I need to parse, and people have been recommending mmap because this should avoid having to allocate the entire file in-memory.</p>
  
  <p>But looking at 'top' it does look like I'm opening the entire file into the memory, so I think I must be doing something wrong. 'top shows >2.1 gig'</p>
  
  <p>This is a code snippet that shows what I'm doing.</p>
  
  <p>Thanks</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;err.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sysexits.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/mman.h&gt;
  #include &lt;cstring&gt;
  int main (int argc, char *argv[] ) {
    struct stat sb;
    char *p,*q;
    //open filedescriptor
    int fd = open (argv[1], O_RDONLY);
    //initialize a stat for getting the filesize
    if (fstat (fd, &amp;sb) == -1) {
      perror ("fstat");
      return 1;
    }
    //do the actual mmap, and keep pointer to the first element
    p =(char *) mmap (0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
    q=p;
    //something went wrong
    if (p == MAP_FAILED) {
      perror ("mmap");
      return 1;
    }
    //lets just count the number of lines
    size_t numlines=0;
    while(*p++!='\0')
      if(*p=='\n')
        numlines++;
    fprintf(stderr,"numlines:%lu\n",numlines);
    //unmap it
    if (munmap (q, sb.st_size) == -1) {
      perror ("munmap");
      return 1;
    }
    if (close (fd) == -1) {
      perror ("close");
      return 1;
    }
    return 0;
  }
  </code></pre>