News for hackers. Stuff that matters.
Re:There is an oom kill count in Linux by gnramires on Sunday December 31, 2023 @09:10AM (#38824129)
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?

Re:There is an oom kill count in Linux by samus on Sunday December 31, 2023 @12:26PM (#38825676)
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.

Re:There is an oom kill count in Linux by hnfong on Sunday December 31, 2023 @10:18AM (#38824591)
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.

Re:There is an oom kill count in Linux by weebull on Sunday December 31, 2023 @11:14AM (#38825042)
The "attempt to save critical data" is a pretty major difference.
Re:There is an oom kill count in Linux by roblabla on Sunday December 31, 2023 @09:24AM (#38824235)
> 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.

Re:There is an oom kill count in Linux by kaba0 on Sunday December 31, 2023 @12:14PM (#38825567)
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.
Re:There is an oom kill count in Linux by samus on Sunday December 31, 2023 @12:30PM (#38825723)
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.
Re:There is an oom kill count in Linux by alpaca128 on Sunday December 31, 2023 @10:11AM (#38824546)
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.
Re:There is an oom kill count in Linux by willcipriano on Sunday December 31, 2023 @09:23AM (#38824232)
> 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.

Re:There is an oom kill count in Linux by rcxdude on Monday January 01, 2024 @06:42AM (#38831190)
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.