How would you identify and debug a memory leak in a long-running PHP CLI script or worker?

Last updated 15 June 2026

  • Log memory_get_usage(true) periodically. A steady upward slope (not a sawtooth) confirms a leak, and snapshots around suspect code localize it.

  • If gc_collect_cycles() drops memory, the issue is circular references. Otherwise look for retained live references like growing static arrays, caches, listeners, or ORM query logs.

  • Use profilers (Xdebug, Blackfire, php-meminfo) to diff heaps, reset state between jobs, and recycle workers after a job or memory limit as a safety net.

  • https://symfony.com/doc/current/messenger.html
  • https://jolicode.com/blog/symfony-messenger-systemd
  • https://opsnepal.com/blog/which-php-memory-leak-patterns-should-trigger-a-review
  • https://medium.com/@laurentmn/debugging-memory-leaks-in-long-running-symfony-applications-d2e89b17a53d
  • https://www.mironsoft.de/en/blog/symfony-doctrine-batch-processing-large-datasets-efficiently
  • https://www.doctrine-project.org/projects/doctrine-orm/en/3.7/reference/batch-processing.html
  • https://butschster.medium.com/the-memory-pattern-every-php-developer-should-know-about-long-running-processes-d3a03b87271c
  • https://themsaid.com/avoiding-memory-leaks-when-running-laravel-queue-workers
All PHP interview questions