Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sometimes my computer starts to behave sluggishly after running too many programs/processes simultaneously, at points almost looking crashed/frozen. Using Debian Linux, is there a way to automatically kill some processes before memory gets too scarce for smooth operation?

share|improve this question
The original question was a little unclear on how to choose processes to kill, so my edit left the method of choosing ambiguous. – killermist Jun 23 '12 at 3:10
I'm guessing your 'slugishness' is due to swapping. You could decrease the amount of swap and let the kernel's OOM killer whack processes when you run out of memory entirely. – Patrick Jun 23 '12 at 4:40
up vote 2 down vote accepted

Basically, you want a daemon that monitors the free memory, and if it falls below a given threshold, it chooses some process and kills them to free up some memory.

while (true) {
    size_t free_memory = get_free_memory();
    if (free_memory < free_memory_threshold) {
        pid_t pid = choose_a_process_to_kill();
        kill(pid, SIGTERM);

An obvious question is: how do you choose processes to kill? An easy answer would be the one with the biggest memory usage, since it's likely that that is the misbehaving "memory hog", and killing that one process will free up enough memory for many other processes.

However, a more fundamental question is: is it really okay to kill such a process to free up memory for others? How do you know that the one big process is less important than others? There's no general answer. Moreover, If you later try to run that big process again, will you allow it to kick out many other processes? If you do, won't there be an endless loop of revenge?

Actually, the virtual memory mechanism is already doing similar things for you. Instead of killing processes, it swaps out some portion of their memory to disk so that others can use it. When the former process tries to use the portion of the memory later, the virtual memory mechanism swaps in the pages back. When this is happening from different process contentiously (which is called thrashing), you need to terminate some processes to free up the memory, or more preferably, supply more memory. When the system starts

share|improve this answer
while( true ) with no breaking condition will make your computer rather warm... – Wojtek Rzepala Jun 23 '12 at 8:41
@WojtekRzepala: Of course, there should be a delay. – musiphil Jun 23 '12 at 15:12

Your Answer


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.