# fork: Cannot allocate memory / ENOMEM

**Code:** `Cannot allocate memory` · **Category:** Linux · **Seen on:** Linux

The kernel refused a memory allocation — RAM and swap are exhausted, or overcommit/cgroup limits block it.

## Meaning

Unlike the OOM killer (which kills a process), this fails the allocation: new processes can’t fork, commands fail. Small VPSs without swap, memory leaks, or a container memory limit cause it.

## Common causes

- RAM full and no swap
- Memory leak in a long-running process
- vm.overcommit_memory=2 strict accounting
- Container/cgroup memory limit

## Quick fix

1. Find the biggest processes: ps aux --sort=-%mem | head
2. Add swap on small servers
3. Fix the leak or raise the limit

## Detailed fix by platform

### Linux

1. 

```bash
free -h
ps aux --sort=-%mem | head
sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile
```


## How to diagnose

1. **Memory** — free -h
2. **Top users** — ps sorted by memory
3. **Limits** — cgroup/container limits, sysctl vm.overcommit_memory

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/linux/cannot-allocate-memory)
