|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2018-10-08 02:40 UTC] ryan dot brothers at gmail dot com
 Description:
------------
I am running PHP 7.2.10 on CentOS 7.  If I run the below script file1.php, the memory usage gradually increases until it hits the memory limit.  The memory usage should remain relatively constant.  It's related to declaring the closure in file2.php, as if I remove the closure, then the memory usage stays constant.
Also, the memory usage stays constant in PHP 5.6, so the issue is perhaps only in PHP 7 and later.
Test script:
---------------
file1.php
<?php
for ($i = 0; $i < 1000000; $i++)
{
	echo $i.' - '.number_format(memory_get_usage())."\n";
	require('file2.php');
}
file2.php:
<?php
$test_function = function()
{
};
Expected result:
----------------
Memory usage stays constant.
Actual result:
--------------
Memory usage increases until it hits the memory limit.
PatchesPull Requests
Pull requests: 
 HistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 17:00:02 2025 UTC | 
Thanks for confirming. If it helps, the same problem also happens if you put "return;" as the first line of file2.php like the below, so it seems related to compiling perhaps, rather than executing. file2.php: <?php return; $test_function = function() { };if (!class_exists(Test::class)) { class Test { } } is leaking too, even if the the if condition is satisfied only once, test https://github.com/php/php-src/pull/7562/files . Seems to be fixed in PHP 8.1, but only when opcache is enabled (which is not by default for CLI).