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.
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.
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.
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.
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.
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.