LightYear
/Docs
DocsTroubleshootingDiagnose and Fix Out of Memory (OOM) Errors

Diagnose and Fix Out of Memory (OOM) Errors

Understand OOM killer behaviour, diagnose memory issues, and prevent OOM crashes.

intermediate
8 min read
LightYear Team
Updated April 24, 2026
memoryoomtroubleshootinglinux
Ready to get started?

What Is the OOM Killer?

When the Linux kernel runs out of memory, it invokes the OOM killer, which terminates processes to free memory. This often causes unexpected application crashes.

Detecting OOM Events

>_BASH
$# Check kernel logs for OOM events
$dmesg | grep -i "out of memory"
$dmesg | grep -i "oom"
$
$# Check system logs
$grep -i "killed process" /var/log/syslog
$journalctl -k | grep -i "oom"

Example OOM log:

[12345.678] Out of memory: Kill process 1234 (python3) score 900 or sacrifice child [12345.679] Killed process 1234 (python3) total-vm:8192000kB, anon-rss:7000000kB

Step 1 — Check Current Memory Usage

>_BASH
$# Overview
$free -h
$
$# Detailed per-process
$ps aux --sort=-%mem | head -20
$
$# Memory map of a process
$pmap -x $PID | tail -5

Step 2 — Identify Memory Leaks

>_BASH
$# Monitor memory growth over time
$watch -n 5 'ps aux --sort=-%mem | head -10'
$
$# Valgrind (for C/C++ programs)
$valgrind --leak-check=full ./myapp

Step 3 — Add Swap Space

>_BASH
$# Create a 4 GB swap file
$fallocate -l 4G /swapfile
$chmod 600 /swapfile
$mkswap /swapfile
$swapon /swapfile
$
$# Make persistent
$echo '/swapfile none swap sw 0 0' >> /etc/fstab
$
$# Verify
$swapon --show

Note: Swap is much slower than RAM. It prevents crashes but degrades performance.

Step 4 — Tune OOM Killer

>_BASH
$# Protect a critical process from OOM killer
$echo -1000 > /proc/$PID/oom_score_adj
$
$# Make a process more likely to be killed first
$echo 1000 > /proc/$PID/oom_score_adj

Step 5 — Set Memory Limits

>_BASH
$# Limit a service's memory usage
$systemctl set-property myapp.service MemoryMax=2G
$
$# Docker container limit
$docker run --memory=4g --memory-swap=4g myimage

GPU OOM Errors

For CUDA out of memory errors, see Optimise GPU Memory Usage.

Was this article helpful?

Your cookie choices for this website

This site uses cookies and related technologies, as described in our privacy policy, for purposes that may include site operation, analytics, and enhanced user experience. You may choose to consent to our use of these technologies, or manage your own preferences. Cookie policy