I learned the other day that Linux really does kill processes if it runs out of memory. I had detached the swap with swapoff command before that happened. Usually, if any process takes up all the memory by a bug, say memory leak in a loop, my system hanged(I know, technically it's just really really slow), not killing the bad process.

How do I prevent this? No software is perfect and I get to run poorly coded programs and encounter this problem every now and then. I have to reset my system every time. Is this just the limitation of modern kernels? I can't just turn off swap because I know the kernel utilises its swap to some extent to utilise physical memory efficiently even if the physical memory is spacious.

Using smaller size of swap might be an option(shorter time before OOM).

migrated from stackoverflow.com Jan 5 '17 at 9:56

This question came from our site for professional and enthusiast programmers.


The kernel's Out Of Memory Killer is used as a last resort. If you provide enough memory and/or swap for the running processes then it won't be triggered.

As your system runs out of physical memory it will start to use swap space. From this point on it will start to slow down, sometimes dramatically. If you have enough active processes trying to use the same physical memory your system will start to spend more time swapping them in and out than it can actually running them.

When swap is close to being exhausted the kernel will start to kill off processes in an attempt to keep the others running. I haven't ever needed to investigate the criteria the OOMK uses to pick processes so I can't describe them to you here.

The solution is one or more of the following

  • Add more physical memory
  • Add more (or some) swap. This can be a disk partition or file
  • Reduce the number or size of the running processes
  • Fix the code in processes with runaway memory

Some other references

It is possible to set a per-process bound on the amount of memory it can use. When it attempts to exceed this the allocation request fails. Typically this results in a program crashing, either on an uncontrolled fashion because it didn't test the return value of the allocation request, or in a controlled manner with a fatal exception.

  • @Ashethehuman your tone is completely unacceptable. If someone misunderstood your question, then you need to make it clearer. Insulting the people who give their time to answer your questions is, to say the least, counterproductive. We expect our users to be nice, that includes you. – terdon Jan 13 '17 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.