QR CookingNotes

CookingNotes

Your Personal Recipe Book

Get it on Google Play
QR FiNoteMe

FiNoteMe

Smart Finance Tracker

Get it on Google Play
linuxserver The “Out of Memory: Kill Process” (OOM Killer) error occurs when Linux runs out of RAM and swap space. The system forcefully kills a process to free memory, often bringing down services unexpectedly.

How to Solve “Out of Memory: Kill Process” Errors on Linux (Beginner-Friendly Guide)

5 Min Read Verified Content

# Understanding the “Out of Memory: Kill Process” Error


When Linux runs low on memory:

  • Physical RAM is full

  • Swap space may also be exhausted

  • Kernel’s OOM Killer selects a process and terminates it to free memory

You’ll often see messages like:

Out of memory: Kill process 1234 (mysqld) score 987 or sacrifice child Killed process 1234 (mysqld) total-vm:123456kB, anon-rss:98765kB, file-rss:1234kB

This usually affects:

  • Database servers

  • Web servers

  • Memory-hungry applications (PHP, Node.js, Python scripts)

  • Containers with limited memory




## Step 1 — Check Which Process Was Killed


The system log /var/log/syslog or dmesg shows OOM events.

dmesg | grep -i "killed process"

or

grep -i "oom" /var/log/syslog

This tells you:

  • Process name

  • PID

  • Memory usage at time of kill




## Step 2 — Check Current Memory and Swap Usage


Run:

free -h

Example output:

total used free shared buff/cache available Mem: 2.0G 1.8G 0.1G 0.1G 0.1G 0.2G Swap: 1.0G 1.0G 0.0G

High usage or 0 free swap → system at risk of OOM.

Check which processes use most memory:

ps aux --sort=-%mem | head -n 10


## Step 3 — Free Up Memory Temporarily


A. Kill unnecessary processes

sudo kill -9 <PID>

Or restart services using too much memory:

sudo systemctl restart apache2 sudo systemctl restart mysql

B. Clear caches (safe)

sudo sync sudo echo 3 > /proc/sys/vm/drop_caches


## Step 4 — Check Swap and Increase if Needed


Check swap partitions:

swapon --show

Add swap if low or missing:

sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile

Make it permanent by adding to /etc/fstab:

/swapfile none swap sw 0 0


## Step 5 — Identify Memory-Hungry Applications


Common culprits:

  • MySQL/PostgreSQL using large buffers

  • PHP/Python scripts in loops

  • Node.js apps without proper memory limits

  • Docker containers with limited memory

Check running Docker containers

docker stats

Check PHP-FPM processes

ps aux | grep php-fpm


## Step 6 — Optimize Applications and Services


  • Databases: Reduce buffer/cache, optimize queries, use connection pooling

  • Web servers: Limit worker processes, enable caching

  • PHP/Python scripts: Avoid memory leaks, process batches instead of all at once

  • Docker: Limit memory per container using --memory flag

Example Docker memory limit:

docker run -d --memory="512m" my-app


## Step 7 — Adjust Linux OOM Behavior


Sometimes, you want to protect critical processes from being killed.

Check OOM score for a process

cat /proc/<PID>/oom_score

Lower the chance of being killed

echo -17 | sudo tee /proc/<PID>/oom_adj

Or for systemd service:

OOMScoreAdjust=-1000


## Step 8 — Monitor Memory to Prevent OOM


Install monitoring tools:

  • Netdata

  • Glances

  • Prometheus + Grafana

Set alerts when memory usage is high.




## Step 9 — Add More Physical RAM


If your server constantly hits OOM despite optimization:

  • Upgrade VPS RAM (1GB → 2GB or more)

  • Add swap as temporary solution, but RAM is faster




## Beginner-Friendly Checklist


Problem TypeDiagnosis CommandFix
OOM Killer event`dmesggrep -i "killed"`
Low RAMfree -hAdd RAM, optimize app, reduce workers
Low swapswapon --showCreate or expand swap
Memory-hungry appsps aux --sort=-%memOptimize, limit, or restart
Docker memorydocker statsLimit container memory
Prevent killing/proc/<PID>/oom_scoreAdjust OOM score


## Conclusion


The “Out of Memory: Kill Process” error can seem alarming, but it’s Linux protecting itself. By following these steps:

  • You can identify the memory-hungry process

  • Free memory temporarily or increase swap

  • Optimize applications to prevent future OOM events

  • Adjust Linux OOM behavior for critical processes

  • Monitor memory usage regularly

Even beginners can manage OOM events confidently, avoiding unexpected service downtime.

Advertisement
Back to Linuxserver