How to Fix “Out of Memory” Error in Linux
Introduction
The “Out of Memory” (OOM) error in Linux can cause processes to crash or the server to become unresponsive. This guide shows you how to diagnose and fix OOM errors to keep your system stable.
Step-by-step Guide
- Check system logs for OOM events
- dmesg | grep -i oomn
- or
- grep -i 'killed process' /var/log/syslog
- dmesg | grep -i oomn
- Check current memory and swap usage
- free -h
- Identify memory-hungry processes
- ps aux --sort=-%mem | head -10
- Add or increase swap space (if needed)
- Create a 2GB swap file:n
- sudo fallocate -l 2G /swapfile
- sudo chmod 600 /swapfile
- sudo mkswap /swapfile
- sudo swapon /swapfile
- Make it permanent:n
- echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
- Create a 2GB swap file:n
- Kill or restart problematic processes
- sudo kill -9 PIDn
- (Replace
PIDwith the process ID from step 3)
- (Replace
- sudo kill -9 PIDn
- Optimize applications and services
- Reduce memory usage in configs (e.g., lower PHP, MySQL, or Java memory limits).
- Restart heavy services after clearing cache.
- Reboot the server if it’s unresponsive
- sudo reboot
Recommendations
- Monitor memory usage regularly with
htoporfree -h. - Set up swap space on all VPS with low RAM.
- Optimize or upgrade applications that use excessive memory.
- Consider upgrading your VPS if OOM errors persist.
Call To Action
eed more RAM or a more powerful VPS? Check out our hosting plans! https://xernode.com/#pricing