In this lesson, you will learn how to proactively influence the OOM (Out of Memory) Killer’s decision-making process by manually adjusting process-specific scoring parameters.
When your system exhausts all physical RAM and swap space, the kernel invokes the OOM Killer to reclaim memory by terminating processes. To decide which victim is the "best" candidate, the kernel assigns each process a score based on memory usage, runtime duration, and privilege levels. However, the kernel lacks context regarding your business-critical applications, which is why we must step in to protect essential services or nominate sacrificial ones.
The Scoring Mechanism
The kernel maintains a per-process value called oom_score_adj. This value is a modifier that ranges from -1000 to +1000.
- A value of
-1000essentially makes the process "OOM-immune," meaning the kernel will never terminate it. - A positive value increases the likelihood that the kernel will choose this process as a target.
Tuning the OOM Score
You can manipulate this score by writing to the /proc/[pid]/oom_score_adj file for any running process. For example, if you want to ensure that your mission-critical database (e.g., mysqld) is never killed, you would identify its Process ID (PID) and set its adjustment:
# Set a high-priority process to be immune to OOM killing
echo -1000 > /proc/1234/oom_score_adj
Conversely, if you have a memory-hungry, non-essential batch processing task that you would prefer the kernel kills first during a crunch, you can assign it a higher positive value:
# Make a background task a preferred target for the OOM Killer
echo 500 > /proc/5678/oom_score_adj
It is vital to use these adjustments judiciously. Setting an overly large application to -1000 might prevent the kernel from freeing enough memory, potentially leading to a system-wide lockup or kernel panic rather than a graceful recovery. Always monitor your memory pressure levels to ensure that your OOM policies align with the actual resource constraints of your infrastructure.
Key Takeaway: By writing values between -1000 and 1000 to /proc/[pid]/oom_score_adj, you override the kernel's automated heuristics to protect critical services or prioritize less important tasks for termination during memory exhaustion.