php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80199 PHP leaking memory by not releasing anonymous function declaration
Submitted: 2020-10-07 17:48 UTC Modified: 2020-10-07 18:25 UTC
From: gilperon at gmail dot com Assigned:
Status: Duplicate Package: *General Issues
PHP Version: 7.4.11 OS: CENTOS 8 and WINDOWS 10
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gilperon at gmail dot com
New email:
PHP Version: OS:

Further comment on this bug is unnecessary.

 

 [2020-10-07 17:48 UTC] gilperon at gmail dot com
Description:
------------
Run the test script. In it I am declaring an anonymous function (because I want to be able to unset it later) using eval (my actual function is much more complicated, and I really need to use eval) and unseting it. If you check memory usage while executing this script you will see it will rise, rise and crash.

What is weird, is that if I execute the same test script but without using eval (code below) it works just fine, no memory leak, no crash... nothing. Apache barely uses any memory.

<?php

set_time_limit(0); 
ini_set('memory_limit','-1');

$aaa = array();

for ($i=0;$i<10000000;$i++) {
	
	$aaa[$i] = function($iduc,$cp_ccp) {
	
		return ($cp_ccp and $iduc);

	};

	unset($aaa[$i]);
	
}

?>

Test script:
---------------
<?php

set_time_limit(0); 
ini_set('memory_limit','-1');

$a = array();

for ($i=0;$i<100000000;$i++) {
	
	eval('
	
		$a[' . $i . '] =
		function($i,$da_ca,$iduc,$cp_ccp) {
		
			return ($cp_ccp[' . $i . '] and $iduc[' . ($i+1) . ']);
	
		};
	
	');

	unset($funcao_testar_retorno_parametros[$i]);
	
}

?>

Expected result:
----------------
Variable should be unsetted and memory released.

Actual result:
--------------
Variable does not get out of memory and in a couple minutes crash Apache with lots of error messages complaining about lack of system memory on error_log.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-07 18:07 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2020-10-07 18:07 UTC] requinix@php.net
Are you using $a or $funcao_testar_retorno_parametros?
 [2020-10-07 18:08 UTC] nikic@php.net
-Status: Feedback +Status: Duplicate
 [2020-10-07 18:08 UTC] nikic@php.net
Duplicate of bug #76982.
 [2020-10-07 18:16 UTC] gilperon at gmail dot com
IT HAS ABSOLUTELY NO RELATION WITH THE OTHER QUESTION!

Since you mother F ** are all lazy, I simplified the code:

<?php

set_time_limit(0); 
ini_set('memory_limit','-1');

for ($i=0;$i<100000000;$i++) {
	
	eval('
	
		$bbb = function() {
		
			return true;
	
		};
	
	');

	unset($bbb);
	
}

?>


<?php

set_time_limit(0); 
ini_set('memory_limit','-1');

for ($i=0;$i<100000000;$i++) {
		
	$bbb = function() {
	
		return true;

	};

    unset($bbb);
    
}

?>


Run both codes above on a Windows 10 + PHP 7.4 and you will see your OS crash. Stupid morons. Anyway, I dont need you to fix this, I found a way around, if you dont wanna fix it, go f** yourself.
 [2020-10-07 18:25 UTC] nikic@php.net
-Block user comment: No +Block user comment: Yes
 [2020-10-07 18:25 UTC] nikic@php.net
This is such a well known open problem that I really don't need to run your code to identify it :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 22:01:30 2024 UTC