Understanding Linux oom_score_adj
The oom_score_adj setting in the Linux operating system
allows administrators to control which processes the kernel terminates
when physical memory is exhausted. When the system faces an
Out-Of-Memory (OOM) condition, the OOM Killer evaluates all running
processes and assigns them a score to decide which one to terminate. By
adjusting oom_score_adj, you can protect critical
services—such as databases, web servers, or remote access daemons—from
being killed, or ensure that non-essential, resource-heavy batch jobs
are terminated first.
The Role of the OOM Killer
Linux uses an opportunistic memory allocation strategy known as overcommitting. The kernel allows processes to request more memory than physically exists, assuming that not all processes will use their allocated memory simultaneously. When this assumption fails and RAM plus swap space are completely depleted, the kernel invokes the OOM Killer to prevent a total system crash.
The OOM Killer calculates a badness score for every process, visible
at /proc/[pid]/oom_score. By default, this score is
primarily proportional to the percentage of total RAM and swap the
process is consuming. The process with the highest score is chosen for
termination to free the maximum amount of memory with the least
operational disruption.
Purpose and Function of
oom_score_adj
The oom_score_adj parameter provides a standardized,
linear mechanism to adjust a process's badness score. It is accessible
per-process through the virtual file system at
/proc/[pid]/oom_score_adj.
The value of oom_score_adj accepts integers ranging from
-1000 to 1000:
-1000(OOM_SCORE_ADJ_MIN): Completely exempts the process from the OOM Killer. The kernel will never select this process for termination during an out-of-memory event, provided other killable processes exist.- Negative values (
-1to-999): Reduce the likelihood of the process being killed by subtracting units from the process's calculated memory footprint score. This is ideal for critical infrastructure daemons (e.g.,sshd, PostgreSQL, system logging). 0: The default value. The process receives no preferential treatment, and its vulnerability to the OOM Killer is determined strictly by its actual resource footprint.- Positive values (
1to1000): Increase the likelihood of termination by artificially inflating the process's score. Setting the value to1000makes the process the highest-priority target whenever the OOM Killer activates. This is suited for disposable workloads, background rendering tasks, or batch compute workers.
Inheritance and Configuration
Child processes inherit the oom_score_adj value of their
parent process across a fork() call. This allows
administrators to set the adjustment on a supervisor or container
runtime, automatically applying the policy to all descendant
processes.
You can modify the score dynamically at runtime by writing directly
to the process's procfs entry:
echo -500 > /proc/<PID>/oom_score_adjFor persistent service management, systemd provides direct support
for this directive within unit files using the
OOMScoreAdjust setting:
[Service]
OOMScoreAdjust=-1000By explicitly defining oom_score_adj, system
administrators replace the kernel's purely heuristic termination choices
with a predictable, business-aligned priority hierarchy during memory
emergencies.