Marcio Cunha

Linux out of memory? How to tune OOM Killer and swapiness

Learn how to configure oom_score_adj and swapiness parameters in Linux to protect critical services from sudden out-of-memory terminations.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • The OOM Killer activates when the Linux kernel completely exhausts physical RAM and available swap space.
  • The oom_score_adj parameter allows administrators to manually adjust the termination priority of running processes.
  • Swapiness controls how aggressively the operating system transfers inactive data from RAM to disk storage.
  • Databases and critical applications require strict protection to prevent abrupt interruptions in production environments.
  • Proactive memory monitoring helps diagnose resource bottlenecks before the system is forced to kill essential services.

Why Linux decides to kill your favorite processes

When a Linux server hits the hard limit of physical memory and swap space runs dry, the system faces an immediate operational collapse. To prevent the entire machine from locking up completely or corrupting the filesystem, a drastic mechanism called the Out of Memory (OOM) Killer steps in. In practice, this kernel component forces a process to terminate instantly to free up RAM. The core issue is that the default operating system criteria are not always smart: it often shuts down the primary database or critical web application instead of an idle background process. To prevent this type of disaster in production, we need to understand how the system makes this decision and how we can directly intervene to protect our most valuable services.

How the kernel calculates who dies first

The kernel assigns every running process a numerical value called the badness score, representing the probability of that program being chosen during the next memory shortage event. The higher this score, the higher the termination priority when RAM runs out. This calculation takes into account the exact amount of memory the application is consuming and the privileges of the executing user. Processes started by regular users tend to accumulate more risk points than vital services running under the root user. However, the automated algorithm of the operating system lacks business context, treating a secondary cache process just like it would treat a monitoring tool, similar to discussions on how to tune Zabbix triggers to reduce false alerts. Relying blindly on default settings is an unnecessary risk for any robust architecture.

Fine-tuning priority with oom_score_adj

The most efficient tool we have to control this behavior is the oom_score_adj file located inside each process directory under /proc. This parameter accepts values ranging from minus one thousand (-1000) up to one thousand (1000). When we configure a process with the minimum value of minus one thousand, we instruct the kernel to grant it absolute immunity, making it untouchable by the OOM Killer under any circumstances. Conversely, positive values drastically raise the termination priority of that specific program. This fine-tuning strategy is comparable to the technical rigor needed when diagnosing complex hardware issues, much like learning how to track board shorts safely to prevent irreversible circuit damage. By isolating critical services, we ensure that secondary tasks sacrifice their resources before affecting the main ecosystem.

To apply this configuration permanently to services managed by systemd, we can add specific directives inside the unit configuration file. Below is a practical example of how to shield a critical service by inserting the adjustment line directly into the systemd service section:

[Service]
ExecStart=/usr/local/bin/my-critical-service
OOMScoreAdjust=-900

This simple modification ensures that even under extreme load pressure and severe memory scarcity, your priority service will be the absolute last to suffer any forced interruption by the operating system.

Controlling swap aggressiveness with swapiness

Another determining factor in Linux memory management is the vm.swappiness parameter, which defines how frequently the kernel should move inactive data from physical RAM to the swap area on disk. The default value in most distributions is sixty, meaning the system starts offloading data to disk long before RAM is fully utilized. In modern servers with high memory capacity and fast SSD storage, such a high value can introduce unnecessary application latencies. Lowering swapiness to values between ten and twenty forces the kernel to prioritize retaining active data in physical memory, engaging disk storage only when strictly necessary. This fine-tuning prevents the system from experiencing inexplicable slowdowns caused by high volumes of read and write operations in the swap area, keeping application performance stable and predictable.

To check the current swapiness value on your server and change it immediately without rebooting the machine, follow the command sequence outlined below:

  1. Check the current swapiness value by reading the kernel configuration file.
  2. cat /proc/sys/vm/swappiness
    
  3. Dynamically change the parameter to your desired value using the sysctl command.
  4. sudo sysctl vm.swappiness=10
    
  5. Make the change permanent by appending the configuration to the system settings file.
  6. echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.d/99-swappiness.conf
    

Conclusion and final thoughts

Managing resource scarcity in Linux environments requires proactive planning and mastery over internal kernel mechanisms. The OOM Killer does not need to be an unpredictable enemy if we know how to steer its decisions through proper score and priority adjustments. Combining an intelligent process protection strategy with balanced swap memory management guarantees operational stability and shields vital applications from unexpected catastrophic crashes during critical moments.

Maintaining control over hardware consumption and refining operating system parameters turns reactive administrators into engineers prepared for any high-load scenario. Implement these recommendations in your staging and production environments, evaluate behavior under stress, and keep your infrastructure resilient and protected at all times.