1

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?

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

3 Answers 3

7

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.

0

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.

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

Just use use zram which is a compressed storage for pages. Various Linux distros (such as Lubuntu, Fedora) as well as ChromeOS and Android have enabled zram by default for years. There's not even the conventional swap by default on Android. Even Windows and macOS nowadays uses compressed virtual memory before resorting to swap

By default zram will consumes almost no memory and only takes the compressed size after data has been paged in, therefore if you're not lacking on memory then zram just lies there for free, like when you use normal swap. But when memory becomes constrained zram will come into use and it'll be far faster than swap because CPUs can decompress data much faster than reading from HDDs and even SSDs. You can check the benchmark results here to see zram with the default lzo algorithm is still faster than and old SSD. You can easily change to lz4 algorithm which is nearly 5 times faster than lzo with a slightly lower compression ratio. Nowadays just switch to Zstd and the speed will blow your mind

There's also zswap which uses a different mechanism, but still stores compressed data on RAM

Further reading

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .