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:

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_adj

For persistent service management, systemd provides direct support for this directive within unit files using the OOMScoreAdjust setting:

[Service]
OOMScoreAdjust=-1000

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