0

I'm trying to catch out of memory exception by the following function:

void allocMemory(buffer& thebuf, size_t size)
{
try
{
         thebuf = buffer(size); // new char[] here
}
catch(bad_alloc& ex)
{
     exception handling... // print some information
         exit(1);
}
}

The handling should display some info and close the program. This work fine on Windows, but on Linux the program just close directly with a "Killed" on terminal. It seems that OOM killer kill my program before the exception can be catched.

Is this normal?

How do I catch out of memory exception on Linux?

P.S. I'm testing on ubuntu 12.04 64 bits, using only 4GB memory for testing, no swap space, language is C++. Also, if I new a large amount of memory at once, the function did work on Linux. However, if I new a little bit every times(ex: in a loop), the program will close eventually with the "Killed" on terminal

Any help would be appreciated.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Ethan Chen
  • 164
  • 1
  • 9

3 Answers3

6

On Linux you will not get any bad_alloc exceptions (unless you change some defaults in your shell).

Linux uses lazy allocation strategy. Any allocation request (when not too large) is going to succeed. The OS will return an address which is not mapped to the virtual memory. The mapping happens when the program actually tries to use the memory. Only at this point the out-of-memory condition can be detected.

However, when this happens, it's too late to report the condition to the program. It is not prepared to handle it. So the OS just kills the program outright.

Google linux out of memory killer for (lots) more information.

You can limit amount of memory available to the program with the rlimit command. Set it low enough so that the program hits the limit before exhausting all available memory.

You can also disable the lazy allocation behaviour system-wide if you want but this is rarely a good thing.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • The explanation is very clear, so it seems impossible to catch OOM error with exception on Linux. What I want to do is give a feedback to user when OOM occur and close the program smoothly, is there a good way to do it on Linux ? – Ethan Chen May 17 '14 at 03:14
  • The process is killed with a signal that cannot be caught, so the best you can do is start another small process that would act as a watchdog. – n. m. could be an AI May 17 '14 at 07:01
2

You can configure Linux to not overcommit memory.

Overcommitting is in general a good thing. Airplane tickets are also oversold, which occasionally results in the plane being overbooked and they may need to change somebody's flight. But generally that doesn't happen. The same applies to applications requesting memory from Linux. Like I said, you can configure Linux not to overcommit memory.

See also this question.

Community
  • 1
  • 1
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Thanks for the link. But since our user may not have root authority, it may not be possible to ask them to configure Linux, is there a better way to detect out of memory on Linux ? – Ethan Chen May 17 '14 at 03:07
0

You want something like this:

try {
    thebuf = new buffer[size];
} catch(std::bad_alloc& ex) {
    std::cout << ex.what();
    exit(1);
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54