Insights

OOM Killer Picked Your Database Instead of the Runaway Script? Understand oom_score_adj

November 16, 2026
0:00
Your database process is gone. MySQL, PostgreSQL, or MongoDB — whatever it was, it stopped, connections dropped, and your application started throwing errors. You check the process list and find the PHP or Python script that was chewing through memory is still running happily. The kernel's OOM killer chose your most important process and left the offender alive. This is not a bug. It is how the OOM killer's scoring works by default, and once you understand the math, you can fix the priority.
Before adjusting anything, confirm the OOM killer actually fired. Misdiagnosing a crash as an OOM event leads to wrong fixes. Check the kernel log:
BASH
dmesg | grep -i 'oom\\|out of memory\\|killed process'
You are looking for lines that say something like Out of memory: Killed process <PID> (mysqld) followed by details about memory state at the time. If your system uses journald, you can also search with journalctl -k --grep='oom\\|killed process'. The kernel prints a table right before the kill line showing every process, its RSS (Resident Set Size — how much physical RAM it actually uses), and its oom_score_adj value. That table is the evidence. Read it carefully: it tells you exactly why the kernel picked your database. If you see no OOM messages but the process still disappeared, look elsewhere — maybe the process hit its own internal memory limit, or systemd's MemoryMax killed it via the cgroup, or it simply crashed. Those are different problems.
When physical RAM and swap are both exhausted, the kernel cannot allocate memory for any process. It has two choices: freeze the entire system, or kill something to free memory. It picks killing. The OOM killer assigns every running process a score from 0 to 1000. Higher score means more likely to die. The score is based on two things:
  1. How much memory the process uses. A process consuming a large fraction of total RAM gets a proportionally high base score. A process using half of total memory gets roughly a base score around 500 (illustrative example).
  2. The oom_score_adj tunable. This is a value from -1000 to +1000 that gets added to the base score. A value of -1000 means "never kill this process." A value of +1000 means "kill this first." The default for most processes is 0.
You can see any process's current score:
BASH
# Check the final computed score (read-only, kernel calculates this)
cat /proc/<PID>/oom_score

# Check the adjustment value (this is what you can change)
cat /proc/<PID>/oom_score_adj
Here is why databases lose. Database engines like MySQL or PostgreSQL are designed to use large amounts of RAM for caching, buffer pools, and shared buffers. That is their job — keeping data in memory makes queries fast. But from the OOM killer's perspective, a process using 2 GB of RAM on a 4 GB server has a massive base score. Meanwhile, a runaway script that allocated memory in small bursts, or forked many small child processes, might have a lower individual score even though it collectively caused the exhaustion. The OOM killer does not understand intent. It does not know which process caused the problem. It picks the process whose death frees the most memory with the least system disruption, weighted by the score. Your database, sitting there with a giant resident set, looks like the best candidate.
You can lower the oom_score_adj of critical processes so the kernel strongly prefers killing something else first. For a running process, write directly to the proc file (requires root):
BASH
# Set oom_score_adj for a running process
echo -500 > /proc/<PID>/oom_score_adj
This is temporary — it resets when the process restarts. For a persistent setting, configure it in the service's systemd unit file. Open the override for your database service (the exact service name depends on your distribution and database): Run systemctl edit mysqld (or mariadb, postgresql, mongod — whatever matches your setup). In the override file, add:
INI
[Service]
OOMScoreAdjust=-500
Then reload and restart: systemctl daemon-reload && systemctl restart mysqld After restart, verify the running process picked up the new value by checking /proc/<PID>/oom_score_adj again.
The range is -1000 to +1000. Some guidelines: -1000 disables OOM killing entirely for that process. Sounds great, but it is dangerous. If every critical service sets -1000 and the system truly runs out of memory, the kernel may have nothing it can kill, and the entire system hangs or panics. Use -1000 only if you have exactly one process that must never die and you are confident other processes can be sacrificed. -500 to -900 is a practical range for databases and other services you want to protect. The process can still be killed in extreme situations, but the kernel will exhaust other candidates first. +500 to +1000 is useful for processes you actively want killed first — batch jobs, cron scripts, background workers that can be restarted safely. A reasonable pattern on a server running a database plus a web server plus various background tasks: set the database to something like -800, the web server to -400, and leave everything else at the default 0. This way the OOM killer targets unprotected processes first, then the web server, and the database last.
Protecting the database is half the fix. The other half is making the process that caused the memory exhaustion more likely to be killed. If you know a particular cron job or worker process tends to consume unbounded memory, raise its oom_score_adj. In a systemd unit:
INI
[Service]
OOMScoreAdjust=500
Or better yet, set a hard memory limit on it so it gets killed by the cgroup before the entire system hits OOM:
INI
[Service]
MemoryMax=512M
This is often a cleaner solution than relying on OOM scoring at all. A MemoryMax limit means the rogue process dies on its own terms, the system never reaches full memory exhaustion, and the OOM killer never fires. If you are running services under systemd, cgroup memory limits are worth exploring — there is a related article on cgroups v2 for resource isolation that covers this in more depth.
"The server has plenty of RAM." Check again. Run free -h and look at the available column, not free. Linux uses unused RAM for disk cache, which is normal and reclaimed on demand. But if available is near zero, you genuinely have no memory left. Also check swap usage — if swap is full or nonexistent, OOM triggers faster. "The database was barely using any memory." The OOM killer looks at RSS, not what the process requested. Check the OOM dump table in the kernel log. It shows the actual RSS of every process at the moment of the kill. Trust that table over top or htop output you saw hours earlier. "I set oom_score_adj but it did not help." Verify the value stuck. If the database forks child processes (some configurations do), each child gets its own oom_score_adj and might still have the default 0. Also, if you set the value on a PID that later restarted, the new PID starts fresh at 0 unless the systemd unit carries the setting. "The OOM killer fires repeatedly." Adjusting oom_score_adj does not add RAM. If the system is genuinely out of memory, the OOM killer will keep firing until enough memory is freed. The real fix is either adding RAM, adding swap, reducing memory consumption, or setting hard limits on the processes that consume too much.
After setting oom_score_adj values, verify the full picture. List every process with its current OOM score and adjustment:
BASH
printf '%-8s %-8s %-6s %s\
' 'PID' 'SCORE' 'ADJ' 'COMMAND'
for pid in /proc/[0-9]*/; do
  p=(basename "pid")
  [ -r "$pid/oom_score" ] && printf '%-8s %-8s %-6s %s\
' \\
    "$p" \\
    "(catpid/oom_score 2>/dev/null)" \\
    "(catpid/oom_score_adj 2>/dev/null)" \\
    "(catpid/comm 2>/dev/null)"
done | sort -k2 -rn | head -20
This shows the top 20 processes by OOM score. Your database should appear lower in the list than before. The processes you want killed first should be near the top. Run this after every relevant service restart to confirm the settings persist.
The OOM killer is not broken when it kills your database — it is doing exactly what its scoring algorithm tells it to do. The fix is straightforward: lower oom_score_adj on services you need to survive, raise it on expendable processes, and ideally set MemoryMax on anything with unpredictable memory usage so the OOM killer never needs to fire at all. None of this replaces actually having enough RAM for your workload, but it ensures that when memory runs out, the kernel kills the right thing.

Join My Newsletter & Get Updates!

Get notified whenever a new technical guide, server infrastructure breakdown, or personal reflection goes live, plus a curated weekly digest.