Fix "fork: Cannot allocate memory" on Linux
fork: Cannot allocate memoryResource temporarily unavailablecannot forkThis means Linux couldn't start a new process — usually you're out of memory, or you've hit a process/thread limit (sometimes a fork bomb or a leaking app). Here is how to recover.
Check memory first
If RAM and swap are exhausted, fork() fails. Check usage:
free -h
# if 'available' is near zero, you're out of memory
Free memory by stopping a runaway process, or add swap — see the OOM killer guide.
Check process / thread limits
You can run out of process slots even with free RAM — a leaking app or a fork bomb spawning endlessly. Count processes and find the culprit:
ps -eLf | wc -l # total threads
ps -eo user,pid,nlwp,cmd --sort=-nlwp | head # top thread users
Recover when you can't even run commands
If the shell itself can't fork, you may need the VNC/console and to kill the offender. Raising ulimit -u / nproc limits helps legit workloads, but find the leak first.
A process spawning copies of itself exhausts the process table fast. Set sensible nproc limits per user so one runaway can't take the whole box down.
Can't fork = out of memory or out of process slots. Check free -h and the process count, kill the runaway, add swap or raise nproc.
Cause: no swap configured at all
Without any swap, a short memory spike that would otherwise be absorbed (slowly) by swap instead fails immediately with this error. Adding even a modest swap file gives the system a buffer for transient spikes, though it's not a substitute for fixing a real leak.
Cause: overcommit settings or cgroup/container memory limits
Inside a container or a cgroup-limited environment, common on VPS and shared hosting, you can hit "Cannot allocate memory" well before the host machine itself is full, because your slice has its own cap. Check any container/cgroup memory limit, not just the host's free -h.
How to prevent fork failures
- Size RAM to your actual workload with some headroom, not right at the edge.
- Add swap as a safety net even on a VPS with plenty of RAM.
- Set per-user nproc limits so a runaway script can't exhaust the whole process table.
- Monitor memory trends over time so a slow leak is caught before it becomes an outage.
Related errors
If processes are being killed rather than failing to start, that's the OOM killer, a related but different symptom. If the disk itself, not RAM, is the constraint, see no space left on device. To see what's actually consuming resources, use checking CPU/RAM/disk usage.
Right-sized VPS resources
Our VDS plans give your workload real RAM and sensible limits, so you don't hit fork errors — protected in Frankfurt.
Frequently asked questions
What causes "fork: Cannot allocate memory"?
Linux couldn't create a new process — either RAM and swap are exhausted, or you've hit the per-user process/thread limit (often a leaking app or a fork bomb).
How do I fix it?
Check free -h; if memory is exhausted, stop the runaway process or add swap. If RAM is fine, you've hit a process limit — find the process spawning too many threads and raise nproc for legit workloads.
What is a fork bomb?
A process that endlessly spawns copies of itself, filling the process table until nothing else can start. Per-user nproc limits prevent one user or app from taking down the whole server.
Does adding swap fully fix "Cannot allocate memory"?
It helps absorb short spikes, but swap is slow disk-backed memory — if the real cause is a steady leak or genuinely needing more RAM, swap only delays the same failure and can make the server sluggish. Treat it as a safety net, not a cure.
Why do I get this error inside a Docker container when the host has free RAM?
Containers commonly have their own memory limit (a cgroup cap) independent of the host. Check the container's configured memory limit rather than the host's free -h.
How do I know if a fork bomb is currently running?
Run ps -eLf | wc -l and watch if the thread count is climbing rapidly with many near-identical process names. A genuine fork bomb needs an immediate kill of the offending process tree, often from console access if the shell itself can't fork.
Go deeper on this
Guides and explainers that pair with this fix — from our guides and blog.
How to Secure a Linux VPS
SSH keys, firewall, updates and least privilege — the day-one checklist.
ReadHow to Harden SSH
Key-only login, no root, and stopping brute-force attacks.
ReadIf Someone Gets Root on Your VPS
Contain, investigate, rotate credentials and rebuild cleanly.
ReadFamous Linux Vulnerabilities
Heartbleed to regreSSHion — what they did and the lesson.
ReadRelated articles
Fix SSH "Connection refused" / "Connection timed out"
SSH "connection refused" vs "timed out" — what each means and the exact steps to fix them.
Read fix Linux & VPSFix "Permission denied" on Linux (chmod & chown Explained)
"Permission denied" on a script or file? Understand chmod/chown and fix it the right way.
Read fix Linux & VPSHow to Check CPU, RAM & Disk Usage on a Linux Server
The essential commands to check CPU, RAM and disk on Linux — and find what's eating them.
Read fix