LightYear
/Docs
DocsTroubleshootingResolve Out of Memory (OOM) Errors

Resolve Out of Memory (OOM) Errors

Diagnose and fix OOM killer events that cause processes to be killed due to insufficient memory.

intermediate
7 min read
LightYear Docs Team
Updated April 24, 2026
oommemoryramswaptroubleshooting
Ready to get started?

The Linux OOM (Out of Memory) killer terminates processes when the system runs out of memory. This guide explains how to diagnose OOM events and prevent them.

Check for OOM Events

>_BASH
$dmesg | grep -i "oom|killed process|out of memory"
OUTPUT
[123456.789] Out of memory: Kill process 12345 (php-fpm) score 892 or sacrifice child
[123456.790] Killed process 12345 (php-fpm) total-vm:456789kB, anon-rss:123456kB
>_BASH
$journalctl -k | grep -i oom | tail -20

Check Current Memory Usage

>_BASH
$free -h
OUTPUT
               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       1.8Gi        45Mi        12Mi       234Mi       123Mi
Swap:             0B          0B          0B
>_BASH
$ps aux --sort=-%mem | head -10

Immediate Fixes

Add Swap Space

>_BASH
$fallocate -l 2G /swapfile
$chmod 600 /swapfile
$mkswap /swapfile
$swapon /swapfile
$echo '/swapfile none swap sw 0 0' >> /etc/fstab

Restart Memory-Leaking Services

If a specific service is leaking memory, restart it:

>_BASH
$systemctl restart php8.1-fpm
$systemctl restart nginx

Tune PHP-FPM Memory Usage

Edit /etc/php/8.1/fpm/pool.d/www.conf:

INI
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 500

Calculate the appropriate pm.max_children:

>_BASH
$# Check average PHP-FPM process memory usage
$ps aux | grep php-fpm | awk '{print $6}' | sort -n | tail -5
OUTPUT
# If average is ~50 MB and you have 2 GB RAM with 512 MB for OS/MySQL:
# Available for PHP: 1.5 GB = 1500 MB
# max_children = 1500 / 50 = 30

Protect Critical Processes from OOM Killer

>_BASH
$# Protect MySQL from being killed
$echo -17 > /proc/$(pgrep mysqld)/oom_adj
$
$# Or use oom_score_adj (modern kernels)
$echo -1000 > /proc/$(pgrep mysqld)/oom_score_adj

[!TIP] If OOM events are frequent, the server needs more RAM. Consider upgrading to a larger plan or optimising your application's 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