| title | Linux fork: Cannot allocate memory | |||||
|---|---|---|---|---|---|---|
| slug | linux-fork-cannot-allocate-memory | |||||
| technologies |
|
|||||
| severity | high | |||||
| tags |
|
|||||
| related |
|
|||||
| last_reviewed | 2026-06-27 |
-bash: fork: Cannot allocate memory
sshd[1042]: fork: retry: Resource temporarily unavailable
runtime: failed to create new OS thread (have 8 already; errno=11)
fork: Cannot allocate memory is what userspace prints when fork()/clone()
fails with ENOMEM (errno 12), and the closely related Resource temporarily unavailable is EAGAIN (errno 11) from hitting a process/thread limit. Despite
the wording, this is often not a RAM shortage β it frequently means the
system or a cgroup has hit its process/thread ceiling (pid_max, the
RLIMIT_NPROC per-user process limit, or a cgroup pids.max). Because creating
new processes is broken, even basic commands (ls, ssh) may fail, making the
host feel completely stuck while top-level services still run.
- linux (process creation, cgroups, resource limits)
high β when forking fails, shells, cron jobs, SSH logins, and any service
that spawns workers all break. It is especially nasty because the tools you'd use
to debug (ssh, new shells, ps via a wrapper) can themselves fail to fork,
risking a host you cannot log into.
- A process/thread leak (fork bomb, runaway worker spawn, leaked threads)
filling the global PID space or the per-user
RLIMIT_NPROC. - A cgroup
pids.maxreached (common in containers β "OOM"-like but it's PIDs). - True memory exhaustion: not enough free + swap to copy-on-write a new address space (overlaps with the OOM situation).
kernel.pid_maxset too low for a high-concurrency workload.RLIMIT_NPROC(ulimit -u) too low for the service user.
fork() must (a) allocate kernel structures and a (copy-on-write) page-table for
the child, and (b) obtain a free PID under kernel.pid_max while respecting the
caller's RLIMIT_NPROC and any cgroup pids.max. If memory cannot be reserved
it returns ENOMEM; if a process/thread count limit is hit it returns
EAGAIN. The two errnos point at different fixes: ENOMEM β free memory or add
swap; EAGAIN β raise the relevant process/thread limit. Note each thread also
counts against RLIMIT_NPROC, so a thread-leaking app trips the process limit.
# Total tasks (processes + threads) vs the global ceiling
ps -eLf | wc -l ; cat /proc/sys/kernel/pid_max
# Per-user process counts β find the user that's leaking
ps -eo user= | sort | uniq -c | sort -rn | head
# The per-user process limit the shell/service has
ulimit -u
# Memory + swap headroom (rules ENOMEM in or out)
free -h
# In a container/cgroup v2: current vs max PIDs
cat /sys/fs/cgroup/pids.current /sys/fs/cgroup/pids.max 2>/dev/null$ ps -eLf | wc -l
32768
$ cat /proc/sys/kernel/pid_max
32768
$ ps -eo user= | sort | uniq -c | sort -rn | head
29110 appuser
Total tasks pinned at pid_max, dominated by one user, points to a process/
thread leak (EAGAIN). If instead free -h shows near-zero available + no swap,
it is a genuine ENOMEM.
-
Identify and stop the leaking process tree. If you can still get a shell, kill the offending parent:
pkill -TERM -u appuser <leaking-process> # then fix the spawn loop
If you cannot fork a new shell, use a built-in (
exec) or the console/OOB. -
For
EAGAIN, raise the right limit:RLIMIT_NPROCvia/etc/security/limits.conf(appuser hard nproc 8192) or the systemd unit (TasksMax=8192), or raisekernel.pid_maxfor global pressure. -
For a container, raise
pids.max(or the orchestrator's PID limit). -
For genuine
ENOMEM, free/right-size memory and add swap (see the OOM error). -
Add
TasksMax=to runaway services so one cannot starve the whole host.
ps -eLf | wc -l # task count drops well below pid_max
bash -c 'echo fork OK' # forking a child succeeds
ulimit -u # the raised nproc limit is in effect- Set
TasksMax=on every service so a fork bomb is contained. - Monitor process/thread counts per user and per cgroup; alert before the cap.
- Fix thread/process leaks at the source; bound worker-pool sizes.
- Keep
pid_maxandRLIMIT_NPROCsized for real concurrency. - Reserve headroom: never run a host at 100% of
pid_max.
linux Β· memory Β· process-limits Β· enomem Β· production