Middle
How does Linux determine which process to terminate?
sobes.tech AI
Answer from AI
Linux uses the OOM (Out-Of-Memory) Killer algorithm to determine which processes to terminate when the system runs out of RAM. This algorithm analyzes various metrics to select processes with the least negative impact on the system when terminated.
Main factors considered by the OOM Killer:
oom_score: A numerical indicator reflecting the likely impact of terminating a process on the system. The higher the score, the more likely the process will be chosen for termination. This score is calculated based on the process's memory consumption, process priority (nice value), and other factors.- Memory usage: Processes consuming large amounts of memory generally have a higher
oom_score. - Process priority: Processes with lower priority (higher nice value) are more likely to be terminated.
- Process lifetime: "Younger" processes may have a higher
oom_score. - Parent process: The OOM Killer tries not to terminate critically important system processes or processes that other important processes depend on.
Users can influence the behavior of the OOM Killer by changing the oom_score_adj value for individual processes. This offset is added to the base oom_score, making the process less or more likely to be selected for termination.
# View current oom_score for process with PID 1234
cat /proc/1234/oom_score
# View current oom_score_adj for process with PID 1234
cat /proc/1234/oom_score_adj
# Set minimum oom_score_adj (-1000) for process with PID 1234
# This practically guarantees that the process will not be terminated by the OOM Killer
echo -1000 > /proc/1234/oom_score_adj
Thus, Linux determines which process to terminate under memory shortage conditions based on the complex OOM Killer algorithm, which weighs various factors to minimize negative impacts on system stability.