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.