How to Solve “Out of Memory: Kill Process” Errors on Linux (Beginner-Friendly Guide)
# 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:
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.
or
This tells you:
-
Process name
-
PID
-
Memory usage at time of kill
## Step 2 — Check Current Memory and Swap Usage
Run:
Example output:
High usage or 0 free swap → system at risk of OOM.
Check which processes use most memory:
## Step 3 — Free Up Memory Temporarily
A. Kill unnecessary processes
Or restart services using too much memory:
B. Clear caches (safe)
## Step 4 — Check Swap and Increase if Needed
Check swap partitions:
Add swap if low or missing:
Make it permanent by adding to /etc/fstab:
## 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
Check PHP-FPM processes
## 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
--memoryflag
Example Docker memory limit:
## Step 7 — Adjust Linux OOM Behavior
Sometimes, you want to protect critical processes from being killed.
Check OOM score for a process
Lower the chance of being killed
Or for systemd service:
## 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 Type | Diagnosis Command | Fix |
|---|---|---|
| OOM Killer event | `dmesg | grep -i "killed"` |
| Low RAM | free -h | Add RAM, optimize app, reduce workers |
| Low swap | swapon --show | Create or expand swap |
| Memory-hungry apps | ps aux --sort=-%mem | Optimize, limit, or restart |
| Docker memory | docker stats | Limit container memory |
| Prevent killing | /proc/<PID>/oom_score | Adjust 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.