Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

We've all experienced it--some program is asked to do something that requires a huge amount of memory. It dutifully tries to allocate all this memory, and the system immediately begins thrashing, swapping endlessly and becoming sluggish or non-responsive.

I most recently experienced this on my Ubuntu laptop due to a Matlab script trying to allocate a ridiculously huge matrix. After ~5+ minutes of thrashing, I was able to Ctrl-F1 to a console and kill Matlab. I would much rather have some hot-key that would have given me control of the system immediately and allowed me to kill the offending process; or, perhaps, simply silently refuse to allocate such a large buffer.

  1. What is the quickest way to regain control of a Linux system that has become nonresponsive or extremely sluggish due to excessive swapping?

  2. Is there an effective way to prevent such swapping from occurring in the first place, for instance by limiting the amount of memory a process is allowed to try to allocate?

share|improve this question
add comment

4 Answers

up vote 7 down vote accepted

Press Alt-SysRq-F to kill the process using the most memory:

  • The SysRq key is usually mapped to the Print key.
  • If you're using a graphical desktop you might need to press Ctrl-Alt-SysRq-F in case pressing Alt-SysRq triggers another action (e.g. snapshot program).
  • If you're using a laptop you might need to press a function key too.
  • For more information read the wikipedia article.
share|improve this answer
add comment

You may be able to press Ctrl-z to suspend the program. Then you can do kill %1 (or whatever the job number is or you can use the PID).

You can use the ulimit command to try to limit the amount of memory available to a process.

share|improve this answer
    
Ctrl-Z is nice, but usually I am running a Matlab GUI and have lost track of the controlling terminal, so have no easy way to issue the Ctrl-Z keypress. It would be nice if the GUI had a hot key to send SIGSTOP to whatever application has focus! –  nibot Feb 22 '11 at 1:21
    
You can run kill -STOP <pid> which will do the same thing as Ctrl-Z. –  hlovdal Feb 24 '11 at 0:30
    
Yes, but the whole problem is that, in such a situation, the system is so non-responsive that it takes a long time (or forever) to get to a command prompt. –  nibot Feb 24 '11 at 1:19
add comment

I've made a prototype script for this purpose - https://github.com/tobixen/thrash-protect

It's so far just a prototype, but running for one day in production it has already saved two servers from needing a hard reboot.

This script does not kill processes, but suspends them temporary.

Of course, "buy more memory" or "don't use swap" are two alternative, more traditional answers on the question "how to avoid thrashing?"

share|improve this answer
add comment

It would be nice if the GUI had a hot key to send SIGSTOP to whatever application has focus!

There is always the classical xkill command (from xorg-x11-apps-7.4-14.fc14.src.rpm on my system). I guess it should not be too difficult to make a clone that sends SIGSTOP instead of killing the target window.

share|improve this answer
    
How can i make xkill start up quickly at the press of some key combination? –  nibot Feb 24 '11 at 1:20
    
I am not sure. I assume both gnome and KDE have some global shortcut functionality that can be used to launch programs. –  hlovdal Feb 24 '11 at 2:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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