Laravel Magazine
Why Long-Running Octane Workers Leak Memory

Why Long-Running Octane Workers Leak Memory

Eric Van Johnson ·

For most of PHP's life, memory management was almost a non-issue for application developers. Every request spun up a fresh PHP process (or reused one from a pool, in the case of PHP-FPM), ran your code, and then the entire process memory was thrown away. If your code leaked a reference here or accumulated an array there, it didn't matter — the leak died with the request. Laravel Octane, RoadRunner, and Swoole change that deal fundamentally, and understanding why requires understanding how PHP actually manages memory.

Reference counting, not a stop-the-world collector

PHP's primary memory management strategy isn't a garbage collector in the way Java or Go developers would recognize it — it's reference counting. Every PHP value (technically, every zval) carries a refcount. When you do $b = $a;, PHP doesn't copy the data; it increments a counter and both variables point at the same underlying value. When a variable goes out of scope or is unset, the refcount drops. When it hits zero, PHP frees the memory immediately, deterministically, without waiting for a collection cycle.

This is why PHP has historically felt memory-efficient without anyone writing memory-management code: as long as your object graph is a tree — parents pointing to children, nothing pointing back — refcounting frees everything as soon as it's unreachable, with no pause and no separate GC pass.

The problem: circular references

Reference counting has exactly one hole: cycles. If object A holds a reference to object B, and B holds a reference back to A, neither refcount ever drops to zero on its own, even after both are unreachable from anywhere else in your code. Left alone, reference counting would leak every circular structure forever.

PHP's actual garbage collector — the piece people mean when they say "PHP has a GC" — exists specifically to solve this one problem. It runs periodically, looking for "root" candidates (objects whose refcount decreased but didn't reach zero, suggesting a possible cycle), and walks the graph to find and collect genuine cycles. This is a real, separate algorithm (an adaptation of a classic cycle-detection approach), and it does introduce a pause when it runs — but under classic PHP-FPM, that pause happens within the lifetime of a single request and the whole arena gets wiped clean at request end regardless of whether the cycle collector caught everything.

Why this matters more under Octane

Laravel Octane keeps your application booted in memory across many requests inside the same worker process, avoiding the cost of bootstrapping the framework from scratch every time. That's the entire performance win — and it's also exactly why circular references become dangerous. There's no "request end" wiping the slate clean anymore. A cycle that would have been silently discarded along with the rest of a dead request's memory now sits in the worker's heap indefinitely, waiting for the cycle collector to catch it — and if it never quite gets caught, or if new cycles are created faster than they're collected, worker memory climbs request after request until something falls over.

Common sources of accidental cycles in Laravel applications:

  • Closures captured in properties that reference the closure's own object. An event listener registered as $this->onSomething = function () use ($this) { ... } inside a long-lived singleton is a classic cycle.
  • Bidirectional relationships held in memory — a parent object holding a child, and that child holding a back-reference to the parent, both stored as properties on singleton or container-bound services rather than transient request objects.
  • Static or singleton caches that accumulate objects across requests and never evict, which isn't technically a cycle but produces the identical symptom: worker memory growing forever.

What actually helps

A few concrete habits matter more under Octane than they ever did under FPM. Avoid storing large or cyclic object graphs on singletons — bind services as bind() rather than singleton() when they don't need to survive between requests, so Octane's request-boundary state reset actually clears them. Use Octane's Octane::listen() hooks (or the stateful middleware behavior) intentionally, and audit anything you register on AppServiceProvider::boot() for objects that capture $this in a closure held elsewhere. And when memory does creep, don't guess — memory_get_usage() logged per request, plus a profiler like Blackfire or Xdebug's memory reporting run against a suspect worker, will tell you what's actually accumulating far faster than reading through service providers hoping to spot the cycle by eye.

None of this means Octane is fragile — production Laravel Cloud, RoadRunner, and Swoole deployments run for days without restarting. It means the assumptions that let you write sloppy object graphs for free under classic PHP no longer hold, and the code that was always technically leaking is now the code that takes your worker down.

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.