Live data from Hacker News

Handling out of memory conditions in C

eli.thegreenplace.net

11–15 of 15 posts

Re: Handling out of memory conditions in C

#11
Checking the malloc return value doesn't always guarantee that you'll be OK.

From the malloc man page (under BUGS):

"By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available."

It goes on to describe how this behavior can be turned off.

Re: Handling out of memory conditions in C

#12
Allocate 1MB of memory upfront. When OOM happens, free that memory and run recovery mechanism.

Similar policy is used in Linux for disk space: 10% of disk space is reserved for root.

PS. Linux OOMKiller already do something similar: it just kills a process to free some memory.

Re: Handling out of memory conditions in C

#13
post #3

The "segfault policy" is an extremely bad idea. Over the past couple years, several major security vulnerabilities have been caused by programs that took the "segfault" path making attacker-controlled array references to NULL pointers. It's not hard to see that if you index something with a standard native integer, 0 + index can mean any word in memory. If you write to that, that's game over. It's for a similar reaso…

> The safest approach is just to configure malloc to abort

This seems like a good approach. Could you offer more details? I can do it with the MALLOC_CHECK_ environment variable in glibc, but is there some better way to do it at link time? In compiling my own code I can use a wrapper, but I'm not sure how to ensure that strdup aborts.

Re: Handling out of memory conditions in C

#14

Allocate 1MB of memory upfront. When OOM happens, free that memory and run recovery mechanism. Similar policy is used in Linux for disk space: 10% of disk space is reserved for root. PS. Linux OOMKiller already do something similar: it just kills a process to free some memory.

Allocate 1MB of memory upfront. When OOM happens, free that memory and run recovery mechanism.

No OOM recovery is fool proof. What if the OOM situation is caused by another process constantly consuming memory? As soon as you free your 1MB overhead, the other process could immediately consume it and again you're in an OOM state.

Granted, the likelyhood of this is slim, but then again, so is an OOM situation period. In the world of virtual memory and swap space, it pretty much takes a rogue process to trigger an OOM error in the first place.

Re: Handling out of memory conditions in C

#15

Allocate 1MB of memory upfront. When OOM happens, free that memory and run recovery mechanism. Similar policy is used in Linux for disk space: 10% of disk space is reserved for root. PS. Linux OOMKiller already do something similar: it just kills a process to free some memory.

Allocate 1MB of memory upfront. When OOM happens, free that memory and run recovery mechanism. No OOM recovery is fool proof. What if the OOM situation is caused by another process constantly consuming memory? As soon as you free your 1MB overhead, the other process could immediately consume it and again you're in an OOM state. Granted, the likelyhood of this is slim, but then again, so is an OOM situation period. In…

Older JVM's behave in such way: in the OOM situation they runs Garbage Collection process to reclaim some unused memory. GC itself requires some memory to operate, so JVM reserves some memory to GC.
Post reply on HN