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.

  • Do you mean "kill the biggest process" command? – Ramesh Apr 30 '14 at 3:24
  • Linux has this. It's called the OOM killer. But you have to have enable memory overcommit (and it's on by default in most distros). – Patrick Apr 30 '14 at 3:35
  • @Patrick overcommit_memory is 0, overcommit_ratio is 50. – Reactormonk Apr 30 '14 at 4:05

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.

  • I didn't know about OOM Killer. I was thinking of a script which continuously monitors the memory usage using top or ps command and if it exceeds certain threshold limit, killing that process using kill command. – Ramesh Apr 30 '14 at 4:57
  • I don't use any swap. – Reactormonk Apr 30 '14 at 6:12
  • to see which process was killed by oom killer: grep -E "oom-killer|Killed process" /var/log/syslog /var/log/syslog.1 – lesmana Apr 30 '14 at 8:32
  • @Tass Oh, i completely glazed over the "I decided to omit the swap" comment, sorry :-(. Though the "manually kill a process" information still stands. However you shouldn't experience slowness when running out of memory. You should experience the kernel just killing things. – Patrick Apr 30 '14 at 13:00
  • @Patrick that happens, but after ~30 minutes. – Reactormonk Apr 30 '14 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.