Long-running PHP workers do not receive the same automatic request boundary as conventional PHP-FPM requests. State and memory can accumulate across many jobs.
Find growth patterns
Measure memory before and after each job, track peak usage, and identify whether memory returns to a stable baseline. A gradual increase may indicate retained references, static caches, unbounded collections, or extension-level resources.
Bound work units
Process large datasets in batches rather than loading everything into memory. Stream files where possible and release references to large objects after use. Avoid retaining full job payloads in global state.
Reset request-specific state
Clear mutable service state, temporary files, output buffers, and database transactions between jobs. Reinitialize components that are not designed for reuse.
Use worker recycling
A maximum job count or memory threshold can recycle workers as a safety measure. Recycling should not replace fixing a genuine leak, but it can limit impact while investigation continues.
Checklist
- Track memory per job and over worker lifetime.
- Process large datasets in bounded batches.
- Release references and temporary resources.
- Reset mutable state between jobs.
- Recycle workers with documented thresholds.