title: Why doesn't this memory eater really eat memory?
body: <p>I want to create a program that will simulate an out-of-memory (OOM) situation on a Unix server. I created this super-simple memory eater:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  unsigned long long memory_to_eat = 1024 * 50000;
  size_t eaten_memory = 0;
  void *memory = NULL;
  
  int eat_kilobyte()
  {
      memory = realloc(memory, (eaten_memory * 1024) + 1024);
      if (memory == NULL)
      {
          // realloc failed here - we probably can't allocate more memory for whatever reason
          return 1;
      }
      else
      {
          eaten_memory++;
          return 0;
      }
  }
  
  int main(int argc, char **argv)
  {
      printf("I will try to eat %i kb of ram\n", memory_to_eat);
      int megabyte = 0;
      while (memory_to_eat &gt; 0)
      {
          memory_to_eat--;
          if (eat_kilobyte())
          {
              printf("Failed to allocate more memory! Stucked at %i kb :(\n", eaten_memory);
              return 200;
          }
          if (megabyte++ &gt;= 1024)
          {
              printf("Eaten 1 MB of ram\n");
              megabyte = 0;
          }
      }
      printf("Successfully eaten requested memory!\n");
      free(memory);
      return 0;
  }
  </code></pre>
  
  <p>It eats as much memory as defined in <code>memory_to_eat</code> which now is exactly 50&nbsp;GB of RAM. It allocates memory by 1&nbsp;MB and prints exactly the point where it fails to allocate more, so that I know which maximum value it managed to eat.</p>
  
  <p>The problem is that it works. Even on a system with 1&nbsp;GB of physical memory.</p>
  
  <p>When I check top I see that the process eats 50&nbsp;GB of virtual memory and only less than 1&nbsp;MB of resident memory. Is there a way to create a memory eater that really does consume it?</p>
  
  <p>System specifications: Linux kernel 3.16 (<a href="http://en.wikipedia.org/wiki/Debian">Debian</a>) most likely with overcommit enabled (not sure how to check it out) with no swap and virtualized.</p>