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 ofoom_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 tunesoom_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.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?
This will create file (/swapfile) of size 1GB (round)
add this file to your swap pool
This will do the job, now you have an extra Gb of swap memory.