Sometimes my Linux desktop, laptop, or server run out of RAM because the experimental software I work with occasionally tries to use more RAM than I have available. When this happens, the system and console hangs for several minutes while Linux thrashes memory between RAM and swap. It seems like Linux is trying very hard to prevent any program from being killed, but in my case, I'd just like Linux to go ahead and kill it so I can get back to work ASAP and rerun it without hard-rebooting or waiting several minutes.
Is there a third-party program I can run in the background that implements the following logic?
when RAM usage is >98%:
kill the process using the most resident RAM
I've tried sudo sysctl vm.overcommit_memory=2
, but it doesn't do what I think is best in my situation. I don't want to kill small processes that innocently allocate 1MB of RAM when RAM is low. I want to kill the single process that's consuming 31.9GB of RAM or something, since that process is obviously at fault, not the small process.
Also, vm.overcommit_memory
is not what I want because vm.overcommit_ratio
corresponds to ratio of virtual memory, not resident memory, divided by total physical memory. So an application that allocates 2GB of memory but doesn't touch any blocks (and therefore doesn't exist in physical RAM) shouldn't be counted toward in the overcommit_ratio.