Docs / Troubleshooting / How to Fix Cannot Allocate Memory Errors on Linux

How to Fix Cannot Allocate Memory Errors on Linux

By Admin · Mar 1, 2026 · Updated Apr 23, 2026 · 25 views · 2 min read

What Causes This Error?

The "Cannot allocate memory" error (errno 12, ENOMEM) occurs when the system cannot satisfy a memory allocation request. This can happen even when free memory appears available due to memory fragmentation or overcommit settings.

Step 1: Check Memory Usage

# Overview of memory and swap
free -h

# Top memory consumers
ps aux --sort=-%mem | head -20

# Detailed memory breakdown
cat /proc/meminfo | grep -E "MemTotal|MemFree|MemAvailable|SwapTotal|SwapFree|Cached|Buffers"

Step 2: Identify the Culprit

# Find processes using the most memory
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -15

# Check OOM killer logs
dmesg | grep -i "out of memory"
journalctl -k | grep -i "oom"

Step 3: Add Swap Space (Quick Relief)

If your Breeze has no swap or insufficient swap:

# Create a 2GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# Make permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Step 4: Tune Overcommit Settings

# Check current setting
cat /proc/sys/vm/overcommit_memory

# 0 = heuristic (default), 1 = always allow, 2 = strict
# For most Breezes, keep at 0 unless running Redis/specific apps
sudo sysctl vm.overcommit_memory=0

Step 5: Optimize Services

  • Reduce Apache/Nginx worker processes to match available RAM
  • Lower MySQL innodb_buffer_pool_size to 50-70% of available RAM
  • Limit PHP-FPM children: set pm.max_children based on average process size
  • Disable or remove unused services

Long-Term Solutions

  • Upgrade your Breeze to a plan with more RAM
  • Implement application-level caching to reduce memory pressure
  • Use a process manager like systemd to set memory limits per service

Was this article helpful?