I recently spent a lot of time trying to debug why a long running build script written in php would run out of memory. The script loops through a large result set instantiating objects using those results and doing various things.

In PHP5.6 garbage collection was cleaning up the objects as we were done with them. Not so it seemed when moving to PHP7.1, memory use would slowly grow on each iteration until the script died.

I tried things like xdebug_debug_zval( 'variable' ); but was at a loss because the refcount showed the garbage collection should work.

Turns out the problem had nothing to do with PHP7.1, it was a coincidence and had to to with using Zend Server and Z-Ray. Zend Server 9.1 added the ability for Z-Ray to monitor scripts run from the command line. Though a useful new feature, this had the side effect of enabling z-ray data collection on the build script where it had not been a factor before.

Thankfully you can disable z-ray and z-ray data collection as needed:

if (function_exists('zray_disable')) {
    zray_disable(true); //true disables data collection
}