// guide
FrankenPHP memory leak in worker mode: causes and fixes
Memory that keeps growing under FrankenPHP almost always comes from state your application keeps between requests in worker mode: a shared service that piles up data, a static cache, a listener added on every request. Here is how to find it and fix it for good.
Last updated: November 2026
The short answer
- Find the service that keeps state:
vendor/bin/igor-php .lists every shared service, vendor/ included, that mutates a property, a static variable or a global setting at runtime. - Fix it in the code: make the service stateless, or implement
ResetInterfaceso Symfony clears it between requests (on Laravel Octane: ascoped()binding or a reset in an Octane listener). - Keep a safety net: restart workers after a number of requests and size memory with
max_threads × memory_limitbelow the available memory.
Why worker mode leaks when PHP-FPM did not
With PHP-FPM, every request boots the application, runs and frees everything. In FrankenPHP worker mode, the application boots once and the same process serves thousands of requests. That is where the speed comes from, and also why code that was harmless now accumulates.
FrankenPHP resets what belongs to the HTTP request ($_GET, $_POST, $_SERVER…), but not $_ENV, static variables, static properties, globals or in-memory caches. Your framework resets what it knows about: Symfony calls reset() on the services tagged kernel.reset, Laravel Octane handles each request in a copy of the application, so what a request adds to the container is discarded, but the singletons resolved while the worker boots are shared by every request, unless octane.flush lists them. Everything else lives as long as the worker.
The usual causes
- A shared service that piles up data in an array property: a cart, a tag manager, a list of pending events, a per-request cache.
- A local static variable used as a cache (
static $cache = [];): it grows with every distinct key the worker receives. - A listener or closure registered on every request (
addListener(…, fn () use ($x))): listeners pile up, and each one keeps its captured variables alive. - Cleanup that never runs: a
__destruct()on a shared service only runs when the worker stops, a Doctrine listener that only clears its state inpostFlushkeeps it when the flush fails. - A long request that loads thousands of Doctrine entities without
$entityManager->clear(): the peak happens inside one request, even if the entity manager is reset afterwards. - Native code: some PHP extensions, or memory allocated outside PHP, are not freed by the garbage collector. Check FrankenPHP's known issues.
State that does not grow is just as dangerous: ini_set(), setlocale() or date_default_timezone_set() change every following request on the worker, and exit() kills the worker instead of ending the request.
How to find the leak
1. Measure per route. A listener on kernel.terminate logs the memory after each request. If memory grows on the same route request after request, the culprit is in that route's services.
#[AsEventListener(KernelEvents::TERMINATE)]
public function onTerminate(TerminateEvent $event): void
{
$this->logger->info('worker memory', [
'route' => $event->getRequest()->attributes->get('_route'),
'memory' => memory_get_usage(true),
]);
}
2. Audit the code with igor-php. The static linter, written in Go, inspects every shared service of the Symfony container (or every singleton of an Octane worker) and reports the exact property that keeps state, the static variables, the missing kernel.reset tags and the calls that kill the worker. It is the tool presented in the official FrankenPHP documentation, under Auditing worker compatibility.
composer require --dev igor-php/igor-php vendor/bin/igor-php .
3. Optionally, prove it at runtime with Igor Doctor (experimental). Its probe replays every route of a Symfony app on one booted kernel, logs users in, submits the forms in rolled-back transactions and reports what each request leaves for the next one: an exposure, a value kept in memory, a growing array or a crash. A Symfony AI agent then proposes the fix, kept only if the leak disappears. Igor Doctor is a proof of concept built for the Symfony AI & MCP Hackathon, not a production tool yet.
How to fix it
Make the service stateless: keep per-request data in the session, the database or the Request, not in a property. If it has to hold state, implement ResetInterface: with autoconfiguration, Symfony tags it kernel.reset and clears it between two requests.
final class CartService implements ResetInterface
{
private array $items = [];
public function add(Product $p): void
{
$this->items[] = $p;
}
public function reset(): void
{
$this->items = [];
}
}
- Replace static arrays with a cache that expires (Symfony's Cache component).
- Register listeners once, with
#[AsEventListener], instead of on every request. - Restore temporary state (locale, timezone) in a
finallyblock. - In batch work, call
$entityManager->clear()regularly and iterate withtoIterable(). - For a leak in a dependency you cannot patch, restart the worker after a number of requests and report it to the maintainer.
Every rule of Igor comes with a fix: see the fix guide or run igor-php rule <RuleId>.
After the deploy: monitor resources (strongly recommended)
A static audit does not replace watching production. After switching to worker mode, monitor the resources of the server and of each worker, at least for the first days and after every deploy. The signal to look for is simple: memory that only goes up, request after request, instead of levelling off after warm-up. Other signs: workers that restart or crash, a queue that grows, response times that drift.
- Ember, recommended by FrankenPHP, is a terminal dashboard for Caddy and FrankenPHP: per-thread state, worker queue and crashes, memory per request and per route. It reads Caddy's admin API (keep it on
localhost:2019, never public) and needs themetricsglobal option. Runemberon the server, orember --daemon --expose :9191to export Prometheus metrics. - Container or server memory over time:
docker statsfor a quick look, your host's dashboard, or Prometheus and Grafana with an alert when memory keeps climbing over several hours. - Memory per route in your logs, with the
kernel.terminatelistener that logs memory after each request: it tells you which route makes memory grow.
When memory climbs: note the routes involved, run vendor/bin/igor-php . on the code deployed, and keep worker restarts as a safety net until the leak is fixed.
The safety net: restarts and memory sizing
- Restart workers after N requests. FrankenPHP has a native
max_requestsoption, still experimental, in thefrankenphpblock of the Caddyfile. Otherwise, FrankenPHP's worker example reads aMAX_REQUESTSenvironment variable, and theruntime/frankenphp-symfonypackage readsFRANKENPHP_LOOP_MAX. - Size the memory: keep
max_threads × memory_limitbelow the memory available to FrankenPHP. - In containers, set
GOMEMLIMITto the container's memory, and prefer glibc (Debian-based images) over musl.
Frequently asked questions
- Is a memory leak under FrankenPHP a bug in FrankenPHP?
- Rarely. In worker mode, FrankenPHP keeps your application in memory on purpose. It resets the superglobals tied to the request, but static variables, static properties, globals and in-memory caches survive. When memory grows request after request, the cause is usually a service of the application or of a dependency that never clears what it stores. Leaks in native extensions exist but are much less common.
- Will restarting the worker after N requests fix my memory leak?
- It hides it. Restarting a worker after a number of requests (FrankenPHP's
max_requestsoption, still experimental, or the setting of your worker script or runtime) frees the memory before it reaches the limit, so it is a good safety net. It does not stop a value written by one request from being read by the next user before the restart: that has to be fixed in the code. - How do I know which service leaks?
- Log
memory_get_usage()per route at the end of each request to see where memory grows, then run igor-php:composer require --dev igor-php/igor-phpandvendor/bin/igor-php .. It names the service and the property that keep state, vendor/ included. Igor Doctor, an experimental hackathon project, goes further and replays every route on one booted kernel to prove what a request leaves for the next one. - How do I monitor FrankenPHP memory in production?
- Monitoring resources after deploying in worker mode is strongly recommended: watch for memory that only goes up instead of levelling off. Ember, a terminal dashboard recommended by FrankenPHP, shows per-thread memory, worker queues and crashes from Caddy's admin API, and can export Prometheus metrics. Add container memory over time (docker stats, Prometheus and Grafana) with an alert on a steady climb.
- Does PHP-FPM have the same problem?
- No. With PHP-FPM, each request starts from scratch and all its memory is freed at the end. Code that was harmless under PHP-FPM can leak as soon as you switch to FrankenPHP worker mode, Laravel Octane, RoadRunner or Swoole.
- How much memory should I give FrankenPHP?
- FrankenPHP recommends keeping
max_threads × memory_limitbelow the available memory. In a container with a memory limit, also set theGOMEMLIMITenvironment variable to the available memory, and prefer an image linked against glibc over musl.