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?


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.

  • 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.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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