1

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?

2
  • The original question was a little unclear on how to choose processes to kill, so my edit left the method of choosing ambiguous. Commented Jun 23, 2012 at 3:10
  • 2
    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. Commented Jun 23, 2012 at 4:40

1 Answer 1

1

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

2
  • 2
    while( true ) with no breaking condition will make your computer rather warm... Commented Jun 23, 2012 at 8:41
  • @WojtekRzepala: Of course, there should be a delay. Commented Jun 23, 2012 at 15:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.