Confirming the OOM kill from logs
BASH
dmesg | grep -i 'oom\\|out of memory\\|killed process'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.
How the kernel decides what to kill
- 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).
-
The
oom_score_adjtunable. 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.
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_adjProtecting a service with oom_score_adj
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_adjsystemctl edit mysqld (or mariadb, postgresql, mongod — whatever matches your setup). In the override file, add:
INI
[Service]
OOMScoreAdjust=-500systemctl daemon-reload && systemctl restart mysqld
After restart, verify the running process picked up the new value by checking /proc/<PID>/oom_score_adj again.
Choosing a value
The other side: making the offender easier to kill
oom_score_adj.
In a systemd unit:
INI
[Service]
OOMScoreAdjust=500INI
[Service]
MemoryMax=512MMemoryMax 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.
Common red herrings when diagnosing OOM events
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.
Verifying your changes work
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 -20Practical takeaway
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.