Hi,<br><br>I&#39;m sorry for a so big email, but this might be a bug.<br><br>As some of you may know, there is a characteristic of malloc called overcommit, which lets a program &quot;allocate&quot; more virtual memory than is available on the system. I&#39;ve set overcommit_memory=2 and turned off swap, but I believe the behaviour of malloc is not the expected.
<br><br>Let me introduce the overcommit, then show the test code, then the results.<br><br>//-- Starting introduction of overcommit<br><br>According to <a href="http://www.die.net/doc/linux/man/man3/malloc.3.html">http://www.die.net/doc/linux/man/man3/malloc.3.html
</a>:<br><br>&quot;By default, Linux follows an optimistic memory
allocation strategy. This means that when <b>malloc</b>() returns
non-NULL there is no guarantee that the memory really is available.
This is a really bad bug. In case it turns out that the system is
out of memory, one or more processes will be killed by the infamous
OOM killer. In case Linux is employed under circumstances where it
would be less desirable to suddenly lose some randomly picked
processes, and moreover the kernel version is sufficiently recent,
one can switch off this overcommitting behavior using a command
like
<dl compact="compact"><dd># echo 2 &gt; /proc/sys/vm/overcommit_memory</dd></dl>See also the
kernel Documentation directory, files
<i>vm/overcommit-accounting</i> and <i>sysctl/vm.txt</i>.&quot;<br><br><br>Looking at vm/overcommit-accounting:<br><br>&quot;The Linux kernel supports the following overcommit handling modes<br><br>0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Heuristic overcommit handling. Obvious overcommits of
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; address space are refused. Used for a typical system. It<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ensures a seriously wild allocation fails while allowing<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; overcommit to reduce swap usage.&nbsp; root is allowed to
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; allocate slightly more memory in this mode. This is the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default.<br><br>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Always overcommit. Appropriate for some scientific<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; applications.<br><br>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Don&#39;t overcommit. The total address space commit
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for the system is not permitted to exceed swap + a<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; configurable percentage (default is 50) of physical RAM.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Depending on the percentage you use, in most situations
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this means a process will not be killed while accessing<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pages but will receive errors on memory allocation as<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; appropriate.<br><br>The overcommit policy is set via the sysctl `vm.overcommit_memory&#39;.
<br><br>The overcommit percentage is set via `vm.overcommit_ratio&#39;.&quot;<br><br>//-- Ending introduction of overcommit<br><br>Then I started doing some tests. Below is my the test code:<br><br>//---------------------------- Top of test code
<br>#include &lt;stdio.h&gt;<br>#include &lt;malloc.h&gt;<br>#include &lt;limits.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;unistd.h&gt;<br><br>int<br>main(void)<br>{<br>&nbsp;&nbsp;&nbsp; int i;<br>&nbsp;&nbsp;&nbsp; char *p;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>4&nbsp;&nbsp;&nbsp; char *r = malloc(1048576);
<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; // Filling some useless space to copy later<br>&nbsp;&nbsp;&nbsp; for (i = 0; i &lt;= 1048576; i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; *(char*)(r+i) = i;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; for (i = 1; ; i++)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; p = malloc(1048576);
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (p == NULL)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf(&quot;malloc %d MB - %p\n&quot;, i, p);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf(&quot;Returned null\n&quot;);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return 0;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf(&quot;malloc %d MB - %p\n&quot;, i, p);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (memcpy(p, r, 1048576) == 0)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf(&quot;Error in memcpy\n&quot;);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; usleep(5);
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp; printf(&quot;Ok, no NULL\n&quot;);<br>&nbsp; return 0;<br>}<br>//---------------------------- End of test code<br><br>Conditions:<br>Kernel: 2.6.9-42.0.10.ELsmp<br>RAM memory: 2GB
<br>Swap: OFF<br>overcommit_memory=2 (changed by me)<br>overcommit_ratio=50 (default)<br><br>&#39; free &#39;&nbsp; yields:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; used&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; free&nbsp;&nbsp;&nbsp;&nbsp; shared&nbsp;&nbsp;&nbsp; buffers&nbsp;&nbsp;&nbsp;&nbsp; cached<br>Mem:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2065252&nbsp;&nbsp;&nbsp;&nbsp; 602872&nbsp;&nbsp;&nbsp; 1462380&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12372&nbsp;&nbsp;&nbsp;&nbsp; 283248
<br>-/+ buffers/cache:&nbsp;&nbsp;&nbsp;&nbsp; 307252&nbsp;&nbsp;&nbsp; 1758000<br>Swap:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<br><br>There is about 1.4GB free RAM memory.<br><br>Then I&#39;ve executed the test code got:<br>malloc 178 MB - (nil)<br>Returned null
<br><br>According to the kernel document above: <br>&quot;The total address space commit for the system is not permitted to exceed swap + a<br>configurable percentage (default is 50) of physical RAM. Depending on the percentage
<br>you use, in most situations this means a process will not be killed while accessing pages<br>but will receive errors on memory allocation as appropriate.&quot;<br><br>Shouldn&#39;t I have got something about 1000 MB instead of 178 MB, because 1000 MB is 50% of&nbsp; physical memory? As long as I have 
1.4GB free, I should be able to allocate those 1000 MB. Are there other (configurable or not) limits for memory allocation? (ulimit is not being used)<br><br>Then I repeated the same test, but with swap on. Swap size is 1GB.
<br><br>Conditions:<br>
Kernel: 2.6.9-42.0.10.ELsmp<br>
RAM memory: 2GB<br>
Swap: 1GB<br>
overcommit_memory=2 (changed by me)<br>
overcommit_ratio=50 (default)<br>
<br>
&#39; free &#39;&nbsp; yields:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; used&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; free&nbsp;&nbsp;&nbsp;&nbsp; shared&nbsp;&nbsp;&nbsp; buffers&nbsp;&nbsp;&nbsp;&nbsp; cached<br>Mem:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2065252&nbsp;&nbsp;&nbsp;&nbsp; 605480&nbsp;&nbsp;&nbsp; 1459772&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12628&nbsp;&nbsp;&nbsp;&nbsp; 283512<br>-/+ buffers/cache:&nbsp;&nbsp;&nbsp;&nbsp; 309340&nbsp;&nbsp;&nbsp; 1755912
<br>Swap:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1048568&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp; 1048568<br><br>Then I&#39;ve executde the test code and got:<br>malloc 1198 MB - (nil)<br>Returned null<br><br>It means this time I&#39;ve allocated successfuly 1198 MB. Shouldn&#39;t I have got something about 2GB?
<br><br><br>Thanks<br>Leonardo Pinheiro<br>