Out of memory is a common issue and the official OOM is not efficient. Several other programs have been introduced to do the killing job faster.

I wonder why there is no approach to creating swap instead of killing. Consider a system with no swap, an OOM program can trigger sudo swapon /swapfile (assuming swapfile exists) instead of killing processes.

Are there technical limitations for implementing this idea?

  • What would be the benefit vs. just activating swap? Sep 20 at 22:15

Programs to do this do exist. Dynamic Swap Daemon for example.

They have to act early - shortly before the swap is needed - because creating, enabling, and disabling swap files are all actions that (like anything else) uses memory, and you really don't want to be doing this when you're already in a low-memory condition.

And any program that monitors the RAM usage to do this as it's needed will also use memory (and should be locked into RAM with something like memlockd so that they're not swapped out when they are needed).

IMO, there's no real benefit in using something like this vs just creating and activating a swap file or partition and forgetting about it. Sounds OK in theory, but pretty useless in practice.


Are there technical limitations for implementing this idea?

No, you can go ahead and program something like that. The issue with swap is that it's very slow, it's very unpredictable, it increases latency, it impedes normal IO operations and in certain situations it does more harm than good, e.g. if you've got an application whose RSS is growing uncontrollably: you try to create swap to accommodate its appetite, the kernel swaps out everything that you're actually using, and the system grinds to a halt.

My advice has always been and remains: add RAM and do not use swap. Swap must be used only when you cannot add RAM out of physical (laptop with no free slots) or financial (shared hosting) limitations.

In Linux, swap can be used for hibernating but that's it. I'm not even convinced by hibernation any more because the tech was created for HDD disks which are very slow for random IO. With today's SSDs there are very few reasons to use hibernation.

  • Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    Sep 22 at 17:26

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.