Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm running Debian on a machine that has limited memory and no option to add swap space. The machine serves as a web and database server. The problem I'm seeing is that when multiple web requests come in, my database stops accepting connections and sometimes even crashes because the system has no more available RAM.

Is it possible to tell Linux to kill specific processes (in my case, Apache) in case another process (in my case, Firebird) requests memory and there is not enough? If it is not possible to do natively, maybe there is some tool to help me achieve this?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

You can tune the OOM killer, to prefer certain processes. Each process has a score that indicates the likelihood of the process being killed in case the system runs in OOM situation. You can see the score of a process in /proc/${PID}/oom_score. You can bias the decision of the OOM killer using /proc/${PID}/oom_adj: a high value increases the probability that OOM killer is going to kill ${PID}. The value of oom_adj is inherited by children so you just set it in the master apache process at startup (in this case you should hava a watchdog that restarts apache); an alternative is to leave the master process at its default level and tune up each child so that the master stays alive and re-spawns the children as needed (in this case you need an external daemon that checks for new children and tunes oom_adj). Of course this assumes that you are using the multi process worker ;-)

Please note that the normal range of oom_adj values (-16,15) is only a bias, the heuristic of the OOM killer may still choose another process if its score is high enough. The special value -17 makes the process unkillable by OOM killer, but it's dangerous, because if the unkillable process (e.g. you DB) goes berserk the kernel may be unable to recover from the memory shortage.

share|improve this answer
    
Brilliant, thanks! –  Rytis May 3 '09 at 16:35
    
This is helpful advice. But if you must tune the OOM killer, you're in big trouble. It helps to manage risk, but you shouldn't be expecting it to run in normal operations (or even under extreme load). –  carlito May 23 '09 at 23:45

If you're using a rented VM, then just add a swap file instead of a swap partition. Lower performance, but at least you'll have the available memory when required :)

How to create a swap file?

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1000000

This will create file (/swapfile) of size 1GB (round)

sudo mkswap /swapfile

add this file to your swap pool

sudo swapon /swapfile

This will do the job, now you have an extra Gb of swap memory.

share|improve this answer
    
It's a VM rented from a provider, so adding extra hardware isn't available. The swap is disabled, and I could not find a way to enable it (it's a custom build of Debian, so it's possible it is completely disabled). We can't really invest into a better plan right now. –  Rytis May 3 '09 at 11:59
    
Alrighty, edited my answer to provide a better solution :) –  saschabeaumont May 3 '09 at 12:39
    
Actually, I've tried that. swapon: /swapfile: Operation not permitted –  Rytis May 3 '09 at 16:29
    
You must run "swapon" as root. –  carlito May 23 '09 at 23:43
    
@Rytis, try putting the file somewhere else. It doesn't have to be in /, it's just a file and can go anywhere. –  saschabeaumont May 25 '09 at 22:15

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.