fork: Cannot allocate memory
ENOMEM: not enough memory
Cannot allocate memory - fork(2)
Your system ran out of available RAM and swap space. It can’t create new processes or allocate more memory.
Fix 1: Check What’s Using Memory
# See memory usage
free -h
# Find the top memory consumers
ps aux --sort=-%mem | head -10
# Or use htop for a live view
htop
Fix 2: Kill Memory-Hungry Processes
# Find and kill the biggest process
ps aux --sort=-%mem | head -5
kill -9 <PID>
# Or kill by name
pkill -f "node server.js"
Fix 3: Add Swap Space
# Check current swap
swapon --show
# Create a 2GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Make it permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Fix 4: Reduce Application Memory Usage
# Node.js — limit heap size
node --max-old-space-size=512 app.js
# Java — limit heap
java -Xmx512m MyApp
# npm install using less memory
npm install --max-old-space-size=512
Fix 5: Docker Container Memory Limit
# docker-compose.yml
services:
app:
deploy:
resources:
limits:
memory: 512M # Container can't exceed this
# Check container memory usage
docker stats
Fix 6: OOM Killer Logs
# Check if Linux OOM killer terminated your process
dmesg | grep -i "out of memory"
journalctl -k | grep -i "oom"
# The OOM killer picks the process using the most memory
# Fix: reduce memory usage or add more RAM