Understanding PHP memory leaks with Blackfire

By Jérôme Vieilledent, on Nov 08, 2019

The Blackfire team attended the Forum PHP 2019 conference, one of the biggest PHP conference in France. We love attending conferences as they are one of the best places to meet developers, to interact with the community, and to have fun! It is also a wonderful opportunity to help each other with technical issues.

Damien Alexandre, co-founder of JoliCode, came to us with this mindset, sharing his memory leak issue with us. He noticed a surprising memory increase while running PHPUnit tests on a Symfony application. He did some profiling and he was able to visualize the “leak” using the Blackfire Timeline view.

Memory leak observed with the Blackfire Timeline view

Romain Neutron, tech lead at Blackfire.io, and Nicolas Grekas, core contributor of Symfony, helped him figuring out the origin of the problem. As Damien wrote on his blog post, the root cause was a bug in PHP itself.

“When the KernelBrowser performs a request, the Symfony Kernel is shut down and (re)booted. This is done to simulate the real experience of an HTTP query received by Symfony: you always have a fresh container. (…)

Loading a container is done either by including existing files, or dumping them before requiring them. Either way, there is an include or a require happening. (…)

(The bug report states) that when you require a file declaring a closure in a loop, the memory usage increases continuously, so it definitely hits hard on Symfony container loading!”

This story tells us two important things:

  1. Blackfire gives you an overview of your application behavior, helping you understand how it works from the inside, like glasses may help you see better 🤓. It can also help spotting bugs in vendor code (e.g. Symfony, Doctrine…), or in PHP itself, even if most of the time you might find workarounds within your own code!
  2. Mutual aid is a fundamental value in Open Source communities. Finding, reproducing, reporting, and fixing issues is always more powerful with team work! As PHP community members, we’re always eager to help.

Happy profiling!

Jérôme Vieilledent

As a Developer Advocate, Jérôme is all about spreading the love! His technical/development background enable him to talk as a peer to peers with our developer users. You’ll read his tips and advices on performance management with Blackfire. And he’ll support you as a day-to-day user.