A PHP-FPM container on managed PaaS starts at 280 MB RSS, looks healthy for six hours, then sits at 1.4 GB. The orchestrator OOM-kills it. A fresh container takes its place and starts the same climb. The leak is real, but “PHP-FPM is leaking” is rarely the right diagnosis — the leak is in something PHP-FPM is hosting.
What’s happening
PHP per se does not leak; each request resets the request-scoped memory arena. What does leak, on a long-lived FPM worker, falls into three buckets:
- The application reuses persistent connections or in-process caches across requests, and never bounds them. A growing
$GLOBALS, a static cache array on a singleton, an unbounded in-memory dedup map. - A PHP extension allocates outside ZendMM and never frees. Common offenders: image-processing extensions (GD, Imagick), older Redis client builds, debug profilers left enabled in production.
- Opcache or APCu is sized larger than the container limit allows for, and grows toward its cap while the application memory also grows.
On a PaaS the container is the boundary. There is no host RAM to absorb the climb; the OOM happens cleanly and the orchestrator restarts you.
How to verify
Inside the container, watch RSS per worker over time:
ps -eo pid,rss,etime,cmd --sort=-rss | grep php-fpm | head
If a single worker’s RSS is climbing while others stay flat, the worker is hosting the leak — which means a long-lived request handler or a specific endpoint is responsible. If all workers climb in lockstep, the leak is extension-level or in shared memory (opcache/APCu).
Then drop pm.max_requests to 200 temporarily and watch. If RSS resets cleanly when each worker recycles at 200 requests, you have confirmed it is per-worker, not extension-level.
For application leaks, run the memprof extension on one worker under shadow traffic. It will surface the call site holding the allocations.
The fix
- Set
pm.max_requeststo a value that bounds the leak’s impact — 500 to 1000 for most apps. This is a circuit breaker, not a cure. - Audit static caches in the application code. Any
static $cache = []that grows without an eviction policy is a leak in waiting. - Update or replace leaky extensions. Imagick in particular has had memory-handling bugs in specific versions; check the version against the project’s tracker.
- Right-size opcache (
opcache.memory_consumption) and APCu against the container memory limit, leaving 30% headroom for the application itself.
Container memory limits make leaks visible quickly, which is good. The fix is at the layer that is actually leaking, not at the FPM tuning.
Stack Harbor’s Cloud PaaS practice handles leak isolation as part of the operating layer — we can take a look.