php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76982 memory leak declaring closure in included file
Submitted: 2018-10-08 02:40 UTC Modified: 2018-10-09 12:07 UTC
From: ryan dot brothers at gmail dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 7.2.10 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-09 12:07 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2018-10-09 12:07 UTC] cmb@php.net
This also happens on current master.  It works fine if file2.php
is inlined.  Neither a debug build nor valgrind
(ZEND_DONT_UNLOAD_MODULES=1 USE_ZEND_ALLOC=0) report a memory
leak, though.
 [2018-10-09 13:21 UTC] ryan dot brothers at gmail dot com
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()
{
};
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Thu Nov 15 03:01:25 2018 UTC