Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

I don't have a lot of memory and sometimes, when I play video games, its get full and my computer start freezing, anything I do take more than 1 minutes before getting done that include mouse move and mouse click.

So what I want is to be able to kill some process I use and I know I can kill (like my Internet browser) before the memory get full so I have little more space left. Do you know a way to do it ?

In technical terms, what I want is that when the memory is almost full it trigger a script that will send a SIGTERM (order to close) to my Internet browser so my PC don't freeze and I can choose to save whatever I'm doing and reopen my browser or forget about my browser.

Thanks for your help.

share|improve this question
up vote 3 down vote accepted

Alright, here goes:

threshold=100          # Minimum amount of memory left when you should start killing, in MB
browser="firefox-bin"  # Change this to whatever you use, firefox is actually firefox-bin
while true; do
    available=$(free -m | head -2 | tail -1 | awk '{print $4}')
    if [ "$threshold" -ge "$available" ]; then
        killall -q $browser  # Will not complain if no processes were killed
    sleep 20

Put this into /home/USERNAME/bin, execute chmod +x ~/bin/SCRIPTNAME, and then add it to your auto-start programs through System->Preferences->Startup Applications.

Then the script will run when you log in, automatically. You can run it manually with ~/bin/SCRIPTNAME &

share|improve this answer
You forgot to have an infinite loop to keep the process running and it's if [ "$threshold" -ge "$available" ]; and not if [ "$available" -ge "$threshold" ];. I have corrected this and added a check up to see if the program is running if ps ax | grep -v grep | grep $browser > /dev/null; then ... and it's working perfectly well. Because the current version of the script doesn't work I'll wait for you to correct it before setting it up as the correct answer. Thank you very much. – Nyamiou The Galeanthrope Oct 12 '10 at 11:47
Oh my god, I completely screwed that one up :D Thanks, I fixed it - also, is it alright if it uses killall -q instead of checking if the process is running? It does not raise any errors for me. – evgeny Oct 12 '10 at 11:54
That should be fixed now. – evgeny Oct 12 '10 at 12:04
Thank you I tested it and it work well. – Nyamiou The Galeanthrope Oct 12 '10 at 12:16

Killing anything to conserve memory is a bad idea, especially automatically. Make sure that you have enough swap and RAM available - of course, running something that uses 1 gig of memory on a 512MB machine is like trying to win a F1 Grand Prix while driving a Corolla.

By which I mean "don't do it."

share|improve this answer
I don't understand why it is such a bad idea. My computer it's extremely slow when it's swapping and most of the time I'll just restart the computer because it's faster than to wait for the PC to unfreeze. What I want is to avoid this situation and I don't want to check the memory left all the time to quit applications myself. – Nyamiou The Galeanthrope Oct 12 '10 at 4:14
Killing anything automatically can result in unpredictable results. If, say, you were working on a report for your employer and decided to play a game, then BOOM! The report is gone. If you can tell me a list of processes that you are likely to run and want to kill, post up a list of those unwanted processes. Otherwise, there's no reliable way for your idea to work. – evgeny Oct 12 '10 at 4:53
Refer to my newest answer (below). – evgeny Oct 12 '10 at 7:02

The kernel does that automatically when you run out of memory.

If you have swap enabled, however, that will take a lot of grinding.

share|improve this answer

Try setting vm.swappiness to 100 as per the instructions here (use 100 instead of 0) On a memory starved machine that can improve performance.

share|improve this answer
Thank you, it seem that it have a good effect on my computer's performances. – Nyamiou The Galeanthrope Oct 12 '10 at 12:20
Glad it helps, it seems to do so here as well even on my 3gb machine when I run a big virtual machine or a game. I would look into compcache as well however I am still able to directly verify if it works. :( – NightwishFan Oct 13 '10 at 6:09

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.