How to Fix Linux Out of Memory (OOM) Killer Issues
In this tutorial, you'll learn about How to Fix Linux Out of Memory (OOM) Killer Issues. We cover key concepts, practical examples, and best practices.
The Problem
Your application or SSH session gets killed without warning. Running dmesg shows Out of memory: Killed process 1234 (mysqld) total-vm:.... The Linux OOM (Out of Memory) killer terminated a process to free memory when the system ran out of available RAM. Without swap space or memory limits, the kernel has no option but to kill processes when memory is exhausted.
Quick Fix
1. Check if it was the OOM killer
dmesg | grep -i "killed process"
Expected output:
[12345.678901] Out of memory: Killed process 4521 (mysqld) total-vm:4213524kB, anon-rss:2142344kB, file-rss:0kB, shmem-rss:0kB
2. Check current memory usage
free -h
Expected output:
total used free shared buff/cache available
Mem: 7.6G 7.2G 124M 234M 302M 87M
Swap: 2.0G 1.8G 212M
If available is very low (under 100 MB), the system is at risk.
3. Add swap space immediately
# Create a 2 GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Make permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
4. Protect critical processes with oom_score_adj
# Find the PID
pgrep mysqld
# Set lower OOM kill priority (-500 means less likely to be killed)
sudo bash -c 'echo -500 > /proc/<PID>/oom_score_adj'
For systemd services:
[Service]
OOMScoreAdjust=-500
5. Set vm.overcommit to strict mode
# Check current setting
sysctl vm.overcommit_memory
# Set to strict (kernel won't overcommit)
sudo sysctl -w vm.overcommit_memory=2
# Make permanent
echo 'vm.overcommit_memory=2' | sudo tee -a /etc/sysctl.conf
6. Limit memory per process
# Using systemd service limits
sudo systemctl set-property my-service.service MemoryMax=1G
sudo systemctl set-property my-service.service MemoryHigh=800M
Common Causes
| Trigger | Description | Prevention |
|---|---|---|
| Memory leak | Application grows indefinitely | Fix the leak, set per-process memory limit |
| No swap configured | System has less total memory | Add swap space with fallocate |
| Overcommit enabled | Kernel allows more allocations than RAM | Set vm.overcommit_memory=2 |
| Too many processes | Many small consumers add up | Limit total processes with systemd |
| Container without limits | Docker container uses all host RAM | Set --memory limits on containers |
Use Debug Mode for Risky Operations
# See every command before it executes
bash -x risky_script.sh
# + sudo sed -i 's/old/new/' /etc/fstab
Always use debug mode when running scripts that modify system configuration like fstab, SELinux settings, or firewall rules. This shows every command before it executes, catching typos early.
Prevention
- Monitor with
free -handhtopand set alerts for low available memory - Size your workloads to leave 20% memory headroom
- Configure OOM score adjustments for critical services in their systemd units
- Always configure swap on production systems (even if swapiness is very low)
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro