php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39010 create_function leaks memory
Submitted: 2006-10-01 15:29 UTC Modified: 2009-09-29 01:00 UTC
Votes:11
Avg. Score:4.5 ± 0.7
Reproduced:10 of 10 (100.0%)
Same Version:4 (40.0%)
Same OS:8 (80.0%)
From: djgrrr at gmail dot com Assigned:
Status: No Feedback Package: Feature/Change Request
PHP Version: 5.1.6 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: djgrrr at gmail dot com
New email:
PHP Version: OS:

 

 [2006-10-01 15:29 UTC] djgrrr at gmail dot com
Description:
------------
The PHP version is actually Irrelevant

If you use create_function many times in a script to make lambda functions, it will use more and more memory.

Now, I know that this is the expected result, but it would be really good to have a function like free_function or delete_function that could be used to remove the lambda functions, and free up the memory they use.

Obviously this function would have to be limited to only lambda function, and it not be possible to use it on normal functions.

Personally, I think this is VERY essential to the CLI version of PHP as many CLI PHP programs do not often restart, but keep running, simply rehashing its config and any modules (using lambda functions)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-03 09:10 UTC] laysoft at gmail dot com
My reproduction code:

<?

ini_set('display_errors',TRUE);
ini_set('memory_limit','2M');
error_reporting(E_ALL);

for ($i=0;$i<30000;$i++) {
	$func=create_function('$m','return($m);');
	unset($func);
	echo '. ';
}

echo '<h1>OK</h1>';

?>

It breaks with "Fatal error: Allowed memory size of 2097152 bytes 
exhausted..." error. Is there any way, to solve this problem?
 [2009-04-03 10:07 UTC] kexianbin at diyism dot com
You could write it like this:
isset($fn)?$fn:$fn=create_function('$v', 'echo $v;');
 [2009-09-21 16:40 UTC] kaja dot fumei at gmail dot com
I've been waiting for a feature like this for a while and I hope it gets added.  It doesn't look very vital from the first example where its generating the same function over and over in a loop.  You could pull it out the loop and save a lot of memory.

But the real reason you need this feature is when the body of code you're creating is dynamic.  I have a library which generates code based on lots of user input and then this code is applied to large data sets.  I've been avoiding create_function because of the "leak" (I do use it in a couple less called spots).  And eval is too slow to run on each row of the data set so I'm forced to generate even more code which includes the looping and everything else and do one very large eval.  I'd rather create a function that is run on every row.  And when the processing is done, I could free that function or I could keep a cache for exact user input matches, but even that would be a limited size and would still need to free old functions.

Yes, create_function is not always needed especially with the new closures.  But it's still the best way to generate dynamic functions and it would be even better if these functions were automatically freed when there are no more references or if there was a function to manually free them.
 [2009-09-21 17:24 UTC] felipe@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2009-09-21 21:59 UTC] kaja dot fumei at gmail dot com
Just built the snapshot on my Fedora 11 machine.  No change.
 [2009-09-29 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 20:01:30 2024 UTC