php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69615 memory leak with eval declaring closure
Submitted: 2015-05-10 02:45 UTC Modified: 2015-05-10 04:28 UTC
From: ryan dot brothers at gmail dot com Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.6.8 OS: Linux
Private report: No CVE-ID: None
 [2015-05-10 02:45 UTC] ryan dot brothers at gmail dot com
Description:
------------
I am seeing a memory leak when using eval() to declare a closure in certain cases.  In the below example, the memory usage increases by about 1MB when it should remain relatively constant.


Test script:
---------------
<?php
function test()
{
	$php_code = '$run = function()
	{';

	foreach (range(1, 15) as $a)
	{
		$php_code .= 'if (1 == 1) { $aaaaaa = true; } else { $aaaaaa = false; }';
	}

	$php_code .= '};';

	eval($php_code);
}

echo number_format(memory_get_usage())."\n";

for ($i = 0; $i <= 1000; $i++)
{
	test();
}

echo number_format(memory_get_usage())."\n";


Expected result:
----------------
224,440
224,440


Actual result:
--------------
224,440
1,271,128


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-10 04:28 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-05-10 04:28 UTC] requinix@php.net
It's not actually a leak; see bug #33487 and bug #68132 for more.

To demonstrate, try this modification:

  echo "0 ", number_format(memory_get_usage())."\n";
  for ($i = 0; $i <= 100000; $i++)
  {
    test();
    if(strlen(rtrim($i, "0")) == 1) {
      echo $i, " ", number_format(memory_get_usage())."\n";
    }
  }

When I run that (5.5.13 on Windows) I get

0 120,360
...
10 142,696
...
100 172,072
200 183,088
300 186,760
400 205,120
500 212,464
600 212,464
700 212,464
800 212,464
900 212,464
1000 212,464
...
10000 212,464
...
100000 212,464

As you can see, memory usage will increase but eventually does plateau.
 [2015-11-17 14:14 UTC] chris at willowsconsulting dot ie
I became interested by the eval() behavior recently, and I can confirm that the memory usage gets higher by the time of the execution.
However it is not really a bug, but the way eval() is fundamentally written:
* it creates a temp file with the code inside (sometimes it is stored in memory)
* it then includes that temp file using a normal include() function

As you can see, the more eval() you do, the more include() it will trigger, resulting in a memory usage that cannot be freed...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC