title: How much memory a program can allocate?
body: <p>How much memory can I allocate for a C++ program running under Linux? In my test case, using <code>new</code> or <code>malloc</code> can allocate more than 170Gb of memory. 
   As a comparison, The same code can only allocate 1.8G in windows and then terminated.</p>
  
  <p>My test machine, one is a virtual machine using virtual box, centos7 64-bit, 2Gb memory. The host is win10 64-bit, memory 8Gb.</p>
  
  <p>Screenshot of using the free command,
  <a href="https://i.stack.imgur.com/ikgWw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ikgWw.png" alt="enter image description here"></a></p>
  
  <p>Below is the test code,</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include &lt;unistd.h&gt;
  
   #define EVERY_ALLOC_MEM 1024 * 1014 // 1Mb
  int main(int argc, char *argv[])
  {
      std::cout &lt;&lt; getpid() &lt;&lt; ":" &lt;&lt; argv[0] &lt;&lt; std::endl;
      for (size_t i = 0; ; i++)
      {
          //char* mem = new char[EVERY_ALLOC_MEM];
          char* mem = (char*)malloc(EVERY_ALLOC_MEM);
          std::cout &lt;&lt; "used " &lt;&lt; i  &lt;&lt; "Mb, that is " &lt;&lt; i * 1024 &lt;&lt; "Kb, and " &lt;&lt; (float)i/1024 &lt;&lt; "Gb"&lt;&lt; std::endl;       
      }
      return 0;
  }
  </code></pre>