Ad Space — Top Banner

Cannot Allocate Memory

Linux Linux

Severity: Moderate

What Does This Error Mean?

The 'cannot allocate memory' error in Linux means the system tried to allocate memory for a new process or operation and there was not enough available — either physical RAM or swap space. When Linux runs out of both, new programs cannot start and existing ones may crash. This is usually caused by a memory-hungry process, a memory leak, insufficient swap space, or a system that simply needs more RAM for its workload.

Affected Models

  • Ubuntu
  • Debian
  • Fedora
  • CentOS
  • Arch Linux
  • Linux Mint
  • openSUSE

Common Causes

  • A runaway process has a memory leak and is consuming all available RAM
  • Too many programs are running simultaneously and the total RAM usage exceeds capacity
  • Swap space is disabled, too small, or the swap partition is full
  • Virtual memory limits (ulimit) for the current user are set too low
  • A kernel parameter prevents the system from using all available RAM

How to Fix It

  1. Check memory usage immediately. Type: free -h to see RAM and swap usage at a glance. Then type: top or htop to see which processes are using the most memory right now.

    Look for any process in top/htop with very high %MEM. A process consuming 80-90% of RAM by itself is almost certainly the cause.

  2. Kill the memory-hogging process. In top, press k, type the process ID (PID) of the problematic process, and press Enter. Confirm with signal 9 (SIGKILL) to force-kill it.

    Killing the process frees its memory immediately. Investigate why it consumed so much memory after the crisis is resolved.

  3. Add or expand swap space. Check if swap is enabled: swapon --show — if empty or small, create a swap file: sudo fallocate -l 2G /swapfile — sudo chmod 600 /swapfile — sudo mkswap /swapfile — sudo swapon /swapfile

    Add to /etc/fstab for persistence: /swapfile none swap sw 0 0 — Swap acts as overflow memory when RAM is full, preventing out-of-memory crashes.

  4. Reduce the number of running services. Type: sudo systemctl list-units --type=service --state=running and disable any services you do not need: sudo systemctl disable --now [service-name]

    On systems with limited RAM (1–2 GB), unnecessary background services eat up the available memory quickly.

  5. Check ulimit settings for the affected user. Type: ulimit -v to see the virtual memory limit. If it is set to a low number, increase it: ulimit -v unlimited — or edit /etc/security/limits.conf for a permanent setting.

    Some managed systems set strict memory limits per user. These limits can cause 'cannot allocate memory' even when the system has free RAM.

When to Call a Professional

If this error appears on a server you manage, it requires investigation of your workload and memory planning. A system administrator can help size RAM correctly and configure swap appropriately for the workload. Persistent memory allocation failures on a properly configured system may also indicate failing RAM hardware.

Frequently Asked Questions

What is the difference between this error and the OOM Killer?

The 'cannot allocate memory' error happens when a process requests memory and the kernel says no immediately. The OOM Killer (Out of Memory Killer) is triggered when the system has already run out of memory and needs to free some by force — it picks a process and kills it automatically. Both have the same root cause (not enough memory) but happen at different stages of the memory shortage.

My system has plenty of RAM but still shows this error. Why?

Even with free RAM, this error can occur if: - The process is requesting a single contiguous block of memory larger than any available contiguous free block - A per-user ulimit (virtual memory limit) is set lower than what the process needs - The system's overcommit setting has been set to conservative mode (vm.overcommit_memory = 2) Check ulimit settings and /proc/sys/vm/overcommit_memory to investigate.

How do I permanently prevent this from happening?

There are three main approaches. First: add or increase swap space so the system has overflow capacity. Second: identify and fix memory leaks in any long-running applications. Third: if the system is genuinely under-resourced for its workload, adding more physical RAM is the permanent solution. Monitoring tools like Prometheus or Netdata can alert you before memory runs out completely.