“Kill the biggest process” button

process

This laptop has an SSD and therefore I decided to omit the swap. This works well in most cases, but sometimes the RAM gets a bit short and the computer gets really sluggish and tends to freeze up. Is there a way to implement a "kill the biggest process" button that goes straight to the kernel in case I notice the freeze fast enough? Or, because it's a computer, a heuristic for when it starts to freeze and shoot the biggest process itself would be fine too.

Best Answer

From your comment, it sounds like the system is just swapping.

Linux has an OOM killer, which is invoked when the system have overcommitted it's memory, and has now run out.
Linux by default performs memory overcommit, which basically means it gives programs more memory than the system actually has. It does this on the assumption that the programs won't actually use all the memory they ask for. However when the system runs out of memory, it already told the various running processes that they have the memory, so it can't just deny it any more. Instead what it does is to invoke the OOM killer. The OOM killer basically finds a process which the kernel thinks will alleviate the out-of-memory condition. Usually this is just the process using the most amount of memory, but the algorithm is actually much more complex than that.

Since you have overcommit_memory set to 0 (automatic mode), the kernel is performing memory overcommit. So from your explained behavior, it sounds like the system is just swapping heavily.

From here there are 2 options.

Reduce swap

Your system is running out of RAM, and so the kernel starts shoving things into swap. If your system runs out of swap, then it invokes the OOM killer. However since you have remaining free swap space, this doesn't happen.

Your original idea, manually kill a process.

You can manually kill a process when you think the system is swapping too much and something needs to die. This can be done through the kernel SysRq triggers.

The kernel has what it calls "magic SysRq". This is a bit of functionality that tells the kernel to perform some sort of emergency operation. This can be things like "remount all volumes read-only", "sync all filesystems", or "reboot now". One of these options is also to invoke the OOM killer.

If your kernel has magic SysRq enabled (kernel option CONFIG_MAGIC_SYSRQ), you can do this in 2 ways.

  1. Alt + SysRq + f
    Simply press these 3 keys on the keyboard.
  2. echo f > /proc/sysrq-trigger
    This will perform the exact same task as the keyboard method, but does so programatically.

You could also disable swap entirely, and this is what I do on most of my systems, and for this exact reason. Swap is beneficial in that the kernel will preemptively swap out data that isn't being used, letting more of your ram be used for caching. But it leads to this forced swapping issue you're seeing.

I personally think the best solution is some sort of kernel option to invoke OOM killer on forced swapping. Basically let preemptive swap work, but if the kernel is forced to move something into swap because you're out of RAM, then invoke OOM killer.
Unfortunately this is just my own personal wish. It does not do this.

Related Question