65 comments

[ 1.7 ms ] story [ 169 ms ] thread
Than never?
In the order of preference, it goes "now", then "never". Presumably "later" is even later, so if you don't find it now, you'd better never find it.
Out of curiosity, what do people recommend for OOM these days on Linux?

My number one recurring issue on Ubuntu is me not noticing the memory usage, running out of memory and locking up my system (forcing a hard reboot and loss of unsaved work)

EDIT: I'd preferably like an OOM handler that would freeze my system and pop up a little menu from which I could select which process to nuke

I've never had that problem. Maybe you don't setup much swap space as I would expect the system to run well enough to save your work and shutdown for most common leaks.
Swap usually makes it worse, without swap there is some chance that the Linux OOM killer does something useful and saves the system. With swap, it becomes a frozen system that never manages to kill anything due to all the swapping. You can wait 5 minutes, 10 minutes, or 15 minutes, but the system never recovers without a reboot.
That would normally mean you ignore the system getting slower for a long time.
Not a long time. With swap enabled, when a process consumes too much memory your system goes from perfect performance to cursor lagging to everything is frozen and you can't even switch to a TTY within 5-10 seconds.

Without swap, the system lags for a couple seconds, OOM killer frees up memory and you're good to go again. The only slowdown is any pages that were kicked out from the file cache. But those quickly come back after the OOM killer does its thing.

What if the thing you kill is in the critical stack to saving your work? If it isn't I don't really understand why you would be swapping it in a lot

I would view the OOm solution as a compute as cattle thinb, but here we are talking about a user desktop where the user can take the best action for themselves once they realize there's a problem.

> If it isn't I don't really understand why you would be swapping it in a lot.

The user doesn't decide which processes are swapped in. If the process gets CPU time and tries to access its data, that data will get swapped in.

> We are talking about a user desktop where the user can take the best action for themselves once they realize there's a problem.

You can't do that with swap, because once you realize there's a problem, you cannot even move your cursor or run commands to take any actions.

This sounds nothing like the gradual leak problem described.. An OOM killer is great once you actually run out of resources, removing virtual resources to always run out and play roulette is using it as a fad hammer.
You have only about 10sec between the system getting slower and the system locking up completely. If you manage to hit the Magic SysRq key combination to trigger OOM manually, that can save the system, but you have to be quick.

[1] https://en.wikipedia.org/wiki/Magic_SysRq_key

That only works if the system is accessing mostly anonymous pages. If the load on the system is accessing plenty of mmapped code/data pages, it can still trash those even if swap is disabled. I've still seen systems hanging for 10+ minutes without recovering even though swap was already disabled.

The Linux kernel OOM killer only acts if there's nothing left that can be discarded, which often happens way too late to save the system. You need a user-mode OOM-killer like earlyoom if you want to keep the system responsive.

Yes, I don't disagree that user-mode OOM-killers are helpful, or that the system can still hang without swap.

I'm just saying turning on swap, or increasing the swap capacity does not fix the problem, and it usually makes it worse.

This is why on production systems important binaries should mlock all of their code pages into ram at startup.
My previous Linux laptop with 8GB of RAM (2014–2017 or so, I think) I never even got round to setting up swap, and I only ran into problems two or three times total (when running two or three Firefox instances and Chromium and a VM taking 2GB and a bunch of other things running—some of the biggest consumers, namely browsers, actually notice if they’re using too much of your RAM and adjust how they work so they don’t ask for so much RAM, at what I believe is a fairly slight performance cost).

On my current laptop and with my current habits, I’m consuming a lot more memory, and so the technique I use to avoid OOM is simply having 40GB of RAM. (As it happens, I do actually have swap set up at present, because certain circumstances meant I wanted to hibernate it occasionally; should disable swap again now I’m back to not needing to hibernate.)

> some of the biggest consumers, namely browsers, actually notice if they’re using too much of your RAM and adjust how they work so they don’t ask for so much RAM

Maybe there are reasons, but it is not nearly good enough -- I frequently run out of RAM and encounter OOM kills (prefer not to deal with linux swap), usually requiring a reboot. I really wish that I could just set an upper limit on (e.g.) firefox RAM usage -- 8GB for example -- instead of its insistence on using all of the unused RAM minus a couple to several hundred MB, which does not leave enough room for memory usage spikes. There might even be a way to set this buried somewhere in the config parameters, but I could never find it. It must be technically possible to set some limit because otherwise the browser would not be able to maintain a somewhat consistent usage just below the total system RAM.

systemd-oomd [0].

You can also use facebook's oomd, but the systemd one is basically the same and slightly better.

If you want, you can also write your own custom memory pressure handler using the recent PSI features: https://docs.kernel.org/accounting/psi.html

The other, easier, answer is to buy more ram, if you simply have more memory than you will ever use (such as having 128GiB of memory when you only regularly use 30GiB), you'll rarely run into OOM issues.

[0]: https://www.freedesktop.org/software/systemd/man/latest/syst...

> The other, easier, answer is to buy more ram, if you simply have more memory than you will ever use (such as having 128GiB of memory when you only regularly use 30GiB), you'll rarely run into OOM issues.

Wanna emphasize this mostly because it took me way too long to think of.

Was dealing with memory pressure regularly and constantly getting frustrated… then dropped an entire $60 or something to throw another 32GB in the machine and never think about my memory again.

Like a week later when I ran out of disk space again and was struggling to make room… same realization. Spent $75 for a 1TB NVME SSD dropped in my mailbox two days later and doubled my storage.

So we’re talking $135 to double my RAM and storage and stop wasting time and brain space on this stuff.

In the words of a past employer..

We ran out of space, and then we realized we are a storage company.

This is one area where Windows is far superior to Linux as a desktop experience.

By accident, I once wrote an infinite loop that just allocated a bunch of memory. As I ran the program my Windows system quickly became laggy and unresponsive. However, not completely. With a bit of patience I got Task Manager up and managed to kill the process and the system was back to normal within a minute.

On Linux, I experience the same as you. Everything is fine until suddenly it's completely unresponsive. Most of the time I can't even manage to shut down the system cleanly and have to hard reset.

Indeed! I wonder why the system can't just stop giving the process memory when it's starting to become critical? (say, you're over 95% memory used)

I really think a more general solution is treating memory (and CPU time as welL!) as a scarce resource, and programs should either: (1) Deal with having severely denied resources (default behavior); (2) Use a communication protocol to negotiate memory with the system.

Negotiation could mean (a) The process freeing memory spontaneously when there's pressure (another process requires it) and it's inactive, (b) Requesting the system free memory from other processes when user requires.

This would impose more memory management burden on developers (trying to fulfill OS requests), but in turn it would make for a far better experience when there's memory contention.

Disclaimer: I have no idea how memory allocation works in detail. Maybe this is reinventing the wheel?

> I wonder why the system can't just stop giving the process memory when it's starting to become critical

If you turn swap off the allocator will error out when you are out of memory. I don't think most software handles that error but all the wiring is there to do that.

> Maybe this is reinventing the wheel?

I believe something like that exists on mobile platforms for memory, at least on iOS you get a message (applicationDidReceiveMemoryWarning:) when the system is memory starved and wants your app to free memory. If you don't release enough memory and the memory pressure doesn't go down, the system will start killing apps.

And on Mac OS you'll get a popup dialog that lets you select programs you want to force quit to free memory. Which is a nice idea as everything else stays responsive, unfortunately it doesn't list any system processes so it's useless if one of them has a bug - which is the reason I even learned of the existence of this oom dialog.
Aren’t it simply more common for apps to go into a “save state and basically exit” mode, from which they are expected to recover without any fuss? Like, ios is very aggressive in “killing” background apps, and since this is the general consensus, apps just behave nicely the majority of time.
Also on Android there are explicit guidelines that processes have to be prepared to be paused, hibernated, or killed at any time. Data in memory is not safe in any way.
Most software doesn't really handle memory allocation failures too well.

malloc() can fail, everyone knows this in theory, but assume you're the programmer handling the malloc() error -- you know the system is likely to have already run out of free memory -- how do you make the program fail gracefully without doing anything at all to allocate memory?

It's not even safe to call printf() since it might have to allocate a new string. There are very few things in modern code that doesn't "incidentally" allocate a couple bytes here and there.

Not to mention languages higher level than C where you don't directly call malloc() but have the language handle it for you (GC etc.). Handling OOM for those programs is even more impossible because you really have no idea whatever class/method/function you're calling is going to trigger an allocation or not.

The best you can do in those cases is try to commit critical data (if any) to disk and have the program die ASAP. Which is not that different from having the kernel kill it for you.

The "attempt to save critical data" is a pretty major difference.
This is already the case. Processes have to ask politely for memory.

The OS could really just say no. But that would commit memory to a process even though it might not actually end up using it, making it unavailable for processes that need it right now. Also, memory allocation latency is higher since the OS has to really do all the bookkeeping up front. Therefore, most Linux distributions by default optimistically grant allocation requests even though there might not be enough memory available yet.

The big disadvantage is that the OS might be unable to actually grant memory. The process then gets OOM killed. This is sort of fine on servers since users usually get assigned limits. But there is something left to be desired for single-user systems.

Processes can at any time return memory to the OS when they don't need it. Usually they do that, but this has the disadvantage that they have to ask the OS again for it, which is quite slow compared to keeping it around.

Few applications are actually able to just release memory on request. It's critical user and internal application data after all. Databases and applications with garbage collectors come closest.

Not much memory usage is optional: in practice most applications need a given amount of memory to do what the user is asking them to do, and so the amount of savings that could be realised by fully implementing this is not likely to be large. The best you could hope for is the application going 'no, you can't do that' as opposed to stopping completely, which in the age of autosaving is not really much difference in practice.

There are already interfaces which can do some of what you are talking about: the most common is memory mapping backed by a file, swap, or filesystem caching. This is the most common form of 'optional' memory usage, and it's generally managed by the kernel. A lot of the kind of thing you are talking about can be mapped onto this abstraction. For cases where backing by a file doesn't make sense (e.g. it's cheaper to regenerate the cached data than to read it back from disk), if you are using a mmap-like interface, you can generally mark pages as 'don't need', which means they may be freed by the kernel if there is memory pressure, but won't be wiped immediately. Again the main issue is the use-cases where this represents a significant percentage of memory usage are pretty slim.

On the other hand, at least in my experience, Windows becomes laggy much sooner than Linux. IMO which of the two behaviors is better really depends on the use case.
I don’t think you can make an apples to oranges comparison here, there are too many differences. But sure, linux is mostly server-workload oriented.
Since Linux has Unix-legacy and is usually intended to be used on servers, there is one well-intended assumption at play here: unless different settings are applied, all processes are in principle created equal. This also applies to GUI applications, and means that a single out-of-control application can lock up the whole system.

That's actually fine on multi-user systems. A competent admin would set up tight limits and prevent one user taking over all resources, and intervenes if they mess up. On single-user machines, this limitation is not there because there is effectively only one user. And since desktop Linux is not a priority for most Linux vendors, this scenario sees comparatively little consideration.

In comparison, on Windows some applications (at least explorer.exe and the task manager) seem to have way higher priority. No matter what else is going on, the user must be able to use these application to rein in other applications. The drawback is that there is no recourse should explorer.exe ever lock up.

Explorer and Task Manager are both shells (along with their CLI sibling shells CMD and Powershell), but Task Manager is special in that Windows Logon is always actively monitoring for CTRL+ALT+DEL.

Windows Logon is superior to all the shells as a core Windows subsystem in that it is the one that executes the shell (usually Explorer) on boot/login and can also execute Task Manager, in addition to its primary duties of tracking and securing user logins (hence the name).

This means even if Explorer has completely froze or has crashed, you can always get to Task Manager via Windows Logon and then kill or execute another instance of Explorer (or CMD or Powershell!) or otherwise force reboot the system from Windows Logon.

Linux has something similar to ctrl+alt+del monitoring in the form of REISUB and alia. But it is much more crude (and nowadays mostly disabled by default).
I’ve noticed this too, make -j and boom hard lock only option is to reboot. Really annoying l! Windows kinda deals with it but it has other issues; I have also noticed this behavior on MacOS.
It should be possible to avoid it by using ulimit to set memory and subprocess limits.
More than 20 years ago, I had frequently the same experience with Linux becoming unresponsive.

Then I have stopped using swap partitions or swap files. For more than 20 years I have never used again any kind of swap, on a large variety of desktops, laptops and servers with Linux. I never had again any problem with unresponsiveness.

Nevertheless, I provision all the computers with generous amounts of DRAM, e.g. there are many years since I have last used a computer with less than 32 GB. In these conditions, OOM events are extremely rare.

While there are people who claim that there still exist cases when swap can be useful, I have never seen any evidence for this claim.

Buying more RAM?
At the risk of this being tongue-in-cheeck: if you have a rogue process, any amount of RAM is going to be eaten up.

(It sometimes happens in software development and it's nice if it doesn't take your machine in swap hell as a result.)

Better yet, download more RAM.
I recently installed Linux on my main desktop again. After freezing up twice in a week, I looked around.

I enabled systemd-oom, but it hasn't been necessary, because I also enabled zram as a swap device. I've seen it compress multiple gb of ram into a few hundred mb and haven't frozen my desktop since.

Enable Alt+SysRq+F and do it manually. Works in seconds every time; the kernel can take days because it's not eager enough when thrashing.

Can have security implications, but the target is chosen in the typical way and screen locks should be setting a score_adj to avoid being picked.

(comment deleted)
I'm using earlyoom on desktop, systemd-oomd on servers.

Due to cgroupv2, systemd-oomd tends to kill entire process groups instead of a single run-away process [1]. It's perfect for servers where almost every process is part of a system service already.

For desktop usage I found this behavior unacceptable. I would sometimes get my entire X session killed when trying something dumb (which is the reason I run such a daemon in the first place), or my editor when spawning a helper subprocess...

earlyoom was the simplest daemon of the bunch I tried, and worked out of the box without special configuration.

[1] Or at least this was the state ~1yr ago.

More RAM. It's the only thing that can reliably prevent it. Magic SysRq key[1] can trigger the OOM manually, which can sometimes rescue the system if you notice it soon enough. But if you run into the situation regularly under normal use, not programs running amok, more RAM is a must.

If you can't upgrade the RAM, zram[2] is also worth a try.

[1] https://en.wikipedia.org/wiki/Magic_SysRq_key

[2] https://en.wikipedia.org/wiki/Zram

Is this still the case on a modern distro using MGLRU and systemd-oomd?
Not really, but not all distros have caught up yet.
Look into oom_kill_allocating_task. There's likely a better solution such as an earlier OOM, but killing the wrong process seems better than locking the system.
In a previous embedded job, I built a daemon watching a couple of “at risk of eating too much memory” processes that were not critical for the actual function of the machine, and killed them proactively when memory started getting low. This prevented the system from going into starvation and spending 10 seconds doing whatever each time an Ethernet packet came in.

As for my pragmatic solution at home: max out the physical RAM…

Tangentially, an issue with the desktop use case is that the main process of a long-running browser is often the first to be killed, instead of one of its content processes, which it can survive losing. This is the case since 2019 as the kernel no longer prefers killing children.[0]

As far as I know, unless a browser manages its oom_score_adj, which none do, a wrapper is insufficient as a workaround and a daemon would have to do so on its behalf, because the value is inherited when forking.

[0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/lin...

I'm on openSUSE TW, and I considered systemd-oom, but the setup/configuration procedure for it is pretty elaborate. So I opted for earlyoom, which worked just a fine and required no manual configuration whatsoever.

In my opinion, if your distro already integrates systemd-oom, like Fedora, it's easier to just use that. Otherwise earlyoom is the next best choice.

Also it's recommended to enable zram. It's much faster than a swap partition or file. The combination of earlyoom and zram made my system much more responsive when it's running short of memory.