Handling Out of memory - Linux

P

Paulo da Silva

Hi!

I have the following question.

If a program, using the new operator, eats a lot of memory, linux kills
it before the method specified at set_new_handler gets called!

Is there a way to handle the out of memory condition in Linux? What is
the best practice?

Thanks.
Paulo
 
J

James Kanze

Do you really ever run out of memory in a real application?

Have your really never seen a program with a memory leak:)?

There are any number of other reasons why one might run out of
memory in a specific application, of course. Regretfully, for
the most frequent one I know, the memory you run out of is
stack, and that's undefined behavior. (I know platform specific
ways of protecting against the error, but nothing within the
language.)
 
M

Markus Schoder

Hi!

I have the following question.

If a program, using the new operator, eats a lot of memory, linux kills
it before the method specified at set_new_handler gets called!

Is there a way to handle the out of memory condition in Linux? What is
the best practice?

At a shell prompt do

$ cat /proc/sys/vm/overcommit_memory

If the value is not 0 you have memory overcommit enabled. This means you
can successfully request more memory than is available in terms of
virtual memory. However eventually your system can run out of memory if
more memory is actually used (i.e. written to) than is available. That
means every write access to unused memory can potentially trigger an out
of memory condition it does not even need to be your program that
triggers it.

To avoid this behaviour do

$ echo 0 >/proc/sys/vm/overcommit_memory

This will cause memory allocations to fail if not enough virtual memory
is available i.e. when you call new and not sometime later when the
memory is actually used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,218
Latest member
NatishaFin

Latest Threads

Top