<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Handling out-of-memory conditions in C</title>
	<atom:link href="http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Wed, 10 Mar 2010 17:32:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Philluminati</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-248684</link>
		<dc:creator>Philluminati</dc:creator>
		<pubDate>Mon, 11 Jan 2010 18:18:41 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-248684</guid>
		<description>The comment by kl is wrong. Linux (not that we&#039;re even targeting that in this post) can have overcommit disabled so that malloc will return 0.

Overcommit works by doubly allocating the same memory pointer to two applications and hoping one of them doesn&#039;t actually write to that memory. it&#039;s surprisingly common allegedly.

Anyway that&#039;s why Torvald&#039;s own git project uses the memset (..., size) function. Not to ensure it&#039;s a certain default value...but to force the kernel into an OOM kill straight away...or not,  rather than resulting in a hard to trace call stack.</description>
		<content:encoded><![CDATA[<p>The comment by kl is wrong. Linux (not that we&#8217;re even targeting that in this post) can have overcommit disabled so that malloc will return 0.</p>
<p>Overcommit works by doubly allocating the same memory pointer to two applications and hoping one of them doesn&#8217;t actually write to that memory. it&#8217;s surprisingly common allegedly.</p>
<p>Anyway that&#8217;s why Torvald&#8217;s own git project uses the memset (&#8230;, size) function. Not to ensure it&#8217;s a certain default value&#8230;but to force the kernel into an OOM kill straight away&#8230;or not,  rather than resulting in a hard to trace call stack.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sumerc</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-225653</link>
		<dc:creator>sumerc</dc:creator>
		<pubDate>Fri, 13 Nov 2009 08:05:11 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-225653</guid>
		<description>To write portable code, one shall not assume malloc() never returns NULL. Even the maintainer of glibc, Ulrich Drepper uses the same approach as git. I am assuming git has taken that code from him, because even the function names are same:) xmalloc. See : people.redhat.com/drepper/optimtut2.ps.gz</description>
		<content:encoded><![CDATA[<p>To write portable code, one shall not assume malloc() never returns NULL. Even the maintainer of glibc, Ulrich Drepper uses the same approach as git. I am assuming git has taken that code from him, because even the function names are same:) xmalloc. See : people.redhat.com/drepper/optimtut2.ps.gz</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-221942</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Sun, 01 Nov 2009 04:45:08 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-221942</guid>
		<description>jldugger: Kl&#039;s comment is spot on.

To write portable code it is wise to check for a NULL upon a malloc return; however, on Linux, with the optimistic allocator enabled (which it is by default), malloc will never return a NULL. Since the memory page is allocated on a write to the pointer, the out-of-memory condition will cause the kernel to kill the process.

The proper action to take in an OOM condition is pretty much up to the application. Some applications can simply be restarted, but a restart is not the right course of action for health care or mission critical applications. I like getting a backtrace of an offending application, so I just let most of my applications just die to get a core file.</description>
		<content:encoded><![CDATA[<p>jldugger: Kl&#8217;s comment is spot on.</p>
<p>To write portable code it is wise to check for a NULL upon a malloc return; however, on Linux, with the optimistic allocator enabled (which it is by default), malloc will never return a NULL. Since the memory page is allocated on a write to the pointer, the out-of-memory condition will cause the kernel to kill the process.</p>
<p>The proper action to take in an OOM condition is pretty much up to the application. Some applications can simply be restarted, but a restart is not the right course of action for health care or mission critical applications. I like getting a backtrace of an offending application, so I just let most of my applications just die to get a core file.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jldugger</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-221423</link>
		<dc:creator>jldugger</dc:creator>
		<pubDate>Fri, 30 Oct 2009 21:34:38 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-221423</guid>
		<description>Have you actually tried to trigger OOM conditions? One day after I got dinged by a professor for not checking malloc returns, I decided to test it out and see what the failure mode was and how to fix it. What I discovered is that the kernels in my testing would kill the process on malloc failure.  You never get a chance at freeing up internal space or exiting cleanly. 

Am I missing something here?</description>
		<content:encoded><![CDATA[<p>Have you actually tried to trigger OOM conditions? One day after I got dinged by a professor for not checking malloc returns, I decided to test it out and see what the failure mode was and how to fix it. What I discovered is that the kernels in my testing would kill the process on malloc failure.  You never get a chance at freeing up internal space or exiting cleanly. </p>
<p>Am I missing something here?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-221376</link>
		<dc:creator>Roger</dc:creator>
		<pubDate>Fri, 30 Oct 2009 19:52:19 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-221376</guid>
		<description>Yes I did read the article (twice).

You are wrong because SQLite does not return NULL to the user.  It returns NULL to the other routines in the SQLite library which then gracefully handle it, sometimes returning the SQLITE_NOMEM error code.  In general a developer using SQLite would never have any reason to call sqlite3_malloc directly.  Additionally SQLite has several uses of memory such as as a cache, keeping an ongoing transaction in memory as much as possible before taking a lock on the database, scratch memory etc.  It will never leak memory.  You can read all the details at http://www.sqlite.org/malloc.html</description>
		<content:encoded><![CDATA[<p>Yes I did read the article (twice).</p>
<p>You are wrong because SQLite does not return NULL to the user.  It returns NULL to the other routines in the SQLite library which then gracefully handle it, sometimes returning the SQLITE_NOMEM error code.  In general a developer using SQLite would never have any reason to call sqlite3_malloc directly.  Additionally SQLite has several uses of memory such as as a cache, keeping an ongoing transaction in memory as much as possible before taking a lock on the database, scratch memory etc.  It will never leak memory.  You can read all the details at <a href="http://www.sqlite.org/malloc.html" rel="nofollow">http://www.sqlite.org/malloc.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthew Dempsky</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-221354</link>
		<dc:creator>Matthew Dempsky</dc:creator>
		<pubDate>Fri, 30 Oct 2009 18:05:07 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-221354</guid>
		<description>Like nonane said, qmail is an example of a server that gracefully handles out-of-memory conditions.  Most of the code when it hits a memory limit will clean up and return a proper error response to the caller, while the main daemon (qmail-send) will print a warning message about memory and sleep for 10 seconds before trying again for some very important allocations (e.g., loading and reloading config files, rewriting some mail addresses, maintaining the undelivered mail priority queues).

djbdns is similar: dnscache allocates all of its cache memory up front, but needs some extra temporary memory for resolving a query and processing DNS response packets.  If any of these requests fail, it will give up on just that individual query, clean up the temporary memory allocated, and print a warning message about it.</description>
		<content:encoded><![CDATA[<p>Like nonane said, qmail is an example of a server that gracefully handles out-of-memory conditions.  Most of the code when it hits a memory limit will clean up and return a proper error response to the caller, while the main daemon (qmail-send) will print a warning message about memory and sleep for 10 seconds before trying again for some very important allocations (e.g., loading and reloading config files, rewriting some mail addresses, maintaining the undelivered mail priority queues).</p>
<p>djbdns is similar: dnscache allocates all of its cache memory up front, but needs some extra temporary memory for resolving a query and processing DNS response packets.  If any of these requests fail, it will give up on just that individual query, clean up the temporary memory allocated, and print a warning message about it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alejandro Weinstein</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-221349</link>
		<dc:creator>Alejandro Weinstein</dc:creator>
		<pubDate>Fri, 30 Oct 2009 17:51:49 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-221349</guid>
		<description>The last version of the Embedded Muse, by Jack Ganssle, talk about this (http://www.ganssle.com/tem/tem183.pdf, go the &quot;Too much optimism&quot; section). An excerpt follows:

&quot;&quot;By the time malloc fails, the system is just plain fubared. I hate deeply convoluted error handling code that checks the return code of malloc....

&quot;* Then goes through horrible contortions to attempt to recover  sanity.
&quot;* And has _never_ been tested.
&quot;* And God help it if it either directly, or anywhere else on it&#039;s  call graph, invokes malloc again! Which it will if it tries to printf anything!

&quot;So what is the correct solution?

&quot;Wrap malloc in a function that checks the return code, and if it fails uses statically pre-allocated space to log a stack trace, then system error and reboot!

&quot;But that sounds just like what you are ranting against! Hear me out to find the correct solution.

&quot;What has gone wrong is you have exceeded the design load for that system. Suppose you were told to design a ferry to carry cars. But you couldn&#039;t get management to decide how many cars it should carry. So you designed it so you could load any number of cars.

&quot;When the ferry starts sinking and taking on water, on every gunwale there is a &quot;water coming in&quot; detector. Attached to each of those detectors is an amazingly complex  one-off uniquely-designed gadget which you can&#039;t test without sinking a loaded ferry (for many highly customised loads), that flings the last few cars into the water!

&quot;One solution is to tell management to get their butts into gear and actually _decide_ on the designed for carrying capacity of the ferry. And then design mechanisms to only allow that many on.

&quot;Your customers are OK with knowing that their system can only handle a finite load.&quot;</description>
		<content:encoded><![CDATA[<p>The last version of the Embedded Muse, by Jack Ganssle, talk about this (<a href="http://www.ganssle.com/tem/tem183.pdf" rel="nofollow">http://www.ganssle.com/tem/tem183.pdf</a>, go the &#8220;Too much optimism&#8221; section). An excerpt follows:</p>
<p>&#8220;&#8221;By the time malloc fails, the system is just plain fubared. I hate deeply convoluted error handling code that checks the return code of malloc&#8230;.</p>
<p>&#8220;* Then goes through horrible contortions to attempt to recover  sanity.<br />
&#8220;* And has _never_ been tested.<br />
&#8220;* And God help it if it either directly, or anywhere else on it&#8217;s  call graph, invokes malloc again! Which it will if it tries to printf anything!</p>
<p>&#8220;So what is the correct solution?</p>
<p>&#8220;Wrap malloc in a function that checks the return code, and if it fails uses statically pre-allocated space to log a stack trace, then system error and reboot!</p>
<p>&#8220;But that sounds just like what you are ranting against! Hear me out to find the correct solution.</p>
<p>&#8220;What has gone wrong is you have exceeded the design load for that system. Suppose you were told to design a ferry to carry cars. But you couldn&#8217;t get management to decide how many cars it should carry. So you designed it so you could load any number of cars.</p>
<p>&#8220;When the ferry starts sinking and taking on water, on every gunwale there is a &#8220;water coming in&#8221; detector. Attached to each of those detectors is an amazingly complex  one-off uniquely-designed gadget which you can&#8217;t test without sinking a loaded ferry (for many highly customised loads), that flings the last few cars into the water!</p>
<p>&#8220;One solution is to tell management to get their butts into gear and actually _decide_ on the designed for carrying capacity of the ferry. And then design mechanisms to only allow that many on.</p>
<p>&#8220;Your customers are OK with knowing that their system can only handle a finite load.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kl</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-221340</link>
		<dc:creator>kl</dc:creator>
		<pubDate>Fri, 30 Oct 2009 16:50:01 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-221340</guid>
		<description>OOM recovery is pointless on linux, because `&lt;code&gt;malloc()&lt;/code&gt;` *NEVER RETURNS 0*. Linux has overcommit and OOM-Killer. 

http://linux-mm.org/OOM_Killer

Your error recovery code will never run. In case of real OOM error, your process may be killed (or another process will be killed to allocate memory for you).

Oh, and this may happen outside of malloc(). You get pointer to memory that isn&#039;t allocated yet (page will be allocated when you access it).</description>
		<content:encoded><![CDATA[<p>OOM recovery is pointless on linux, because <code class="backtick">malloc()</code> *NEVER RETURNS 0*. Linux has overcommit and OOM-Killer. </p>
<p><a href="http://linux-mm.org/OOM_Killer" rel="nofollow">http://linux-mm.org/OOM_Killer</a></p>
<p>Your error recovery code will never run. In case of real OOM error, your process may be killed (or another process will be killed to allocate memory for you).</p>
<p>Oh, and this may happen outside of malloc(). You get pointer to memory that isn&#8217;t allocated yet (page will be allocated when you access it).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nolan</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-221335</link>
		<dc:creator>Nolan</dc:creator>
		<pubDate>Fri, 30 Oct 2009 16:24:37 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-221335</guid>
		<description>It&#039;s nice seeing how tried-and-tested real world code handles memory allocation. Another interesting comparison would be how the various memory-safe language implementations handle their mallocs.

If you wrote more posts about the inner workings of common projects, I for one wouldn&#039;t complain!</description>
		<content:encoded><![CDATA[<p>It&#8217;s nice seeing how tried-and-tested real world code handles memory allocation. Another interesting comparison would be how the various memory-safe language implementations handle their mallocs.</p>
<p>If you wrote more posts about the inner workings of common projects, I for one wouldn&#8217;t complain!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Earl Lapus</title>
		<link>http://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c/comment-page-1/#comment-221328</link>
		<dc:creator>Earl Lapus</dc:creator>
		<pubDate>Fri, 30 Oct 2009 15:51:01 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1976#comment-221328</guid>
		<description>I use the if-malloc-returned-null-then-exit approach for as long as I can remember. I guess it&#039;s because that approach was the usual idiom that I grew accustomed to. But, after reading this I thought about how often such OOM errors occur AND looking back, I haven&#039;t really encountered/experienced any of my C programs running out of memory. Funny... it&#039;s like having a bomb shelter in a time and place where not a single bomb exists.</description>
		<content:encoded><![CDATA[<p>I use the if-malloc-returned-null-then-exit approach for as long as I can remember. I guess it&#8217;s because that approach was the usual idiom that I grew accustomed to. But, after reading this I thought about how often such OOM errors occur AND looking back, I haven&#8217;t really encountered/experienced any of my C programs running out of memory. Funny&#8230; it&#8217;s like having a bomb shelter in a time and place where not a single bomb exists.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
