PHP error guide
PHP process cannot allocate memory: meaning and fix
Error summary
Measure process and host memory, reduce concurrency or peak allocation, and size limits from evidence rather than simply raising memory_limit.
What it means
Measure process and host memory, reduce concurrency or peak allocation, and size limits from evidence rather than simply raising memory_limit.
What the error means
The operating system or allocator refused a memory mapping; this differs from PHP’s own “Allowed memory size exhausted” limit message.
Why PHP produces it
The process, container, cgroup, swap policy, or host lacks an allocatable region for the requested memory even if PHP memory_limit has not fired.
PHP version notes
The behavior described for PHP process cannot allocate memory applies to PHP 7.4–8.4 unless a narrower version is stated; exact wording can vary by SAPI and patch release.
Most common causes
- A container or cgroup memory limit is reached.
- Too many FPM workers have high simultaneous resident memory.
- A large allocation cannot be satisfied because of host pressure or address-space constraints.
Minimal examples
BAD — reproduces the problem
$rows = iterator_to_array($repository->yieldRows());
FIXED — safer pattern
foreach ($repository->yieldRows() as $row) { process($row); }
Step-by-step diagnosis
- Compare PHP memory_get_peak_usage with process RSS and cgroup counters.
- Check kernel or container OOM events at the failure time.
- Measure per-worker memory before changing FPM concurrency.
Fixes
Reduce peak and bound concurrency
Stream large datasets and set worker counts from measured resident memory.
foreach ($repository->yieldRows() as $row) {
process($row);
}
Common mistakes when fixing it
- Setting memory_limit=-1 without measuring host capacity.
- Increasing pm.max_children under memory pressure.
- Assuming PHP heap metrics include all extension and process memory.
How to prevent it
- Monitor RSS, cgroup memory, and request concurrency.
- Stream large files and query results.
- Load-test peak-memory workflows with realistic data.
Web server / environment notes
fpm, cli, docker, linux. Host and container limits can fail before PHP memory_limit.
Tags: fpm,cli,docker,linux