title: Why this weird behaviour of memory allocation
body: <p>This interesting code always allocates 3 GB memory in Linux systems even if the physical RAM is less than 3 GB. </p>
  
  <p>How? (I have a 2.7 GB RAM in my system and  this code allocated 3.054 MB memory!)</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;string.h&gt;
      #include &lt;stdlib.h&gt;
      int main(int argc, char *argv[])
      {
          void *ptr;
          int n = 0;
          while (1) {
              // Allocate in 1 MB chunks
              ptr = malloc(0x100000);
              // Stop when we can't allocate any more
              if (ptr == NULL)
                  break;
              n++;
          }
          // How much did we get?
          printf("malloced %d MB\n", n);
          pause();
      }
  </code></pre>