#39010 [Com]: create_function leaks memory

2009-09-21 Thread kaja dot fumei at gmail dot com
 ID:   39010
 Comment by:   kaja dot fumei at gmail dot com
 Reported By:  djgrrr at gmail dot com
 Status:   Open
 Bug Type: Feature/Change Request
 Operating System: *
 PHP Version:  5.1.6
 New Comment:

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.


Previous Comments:


[2009-04-03 10:07:08] kexianbin at diyism dot com

You could write it like this:
isset($fn)?$fn:$fn=create_function('$v', 'echo $v;');



[2008-09-03 09:10:57] 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;$i3;$i++) {
$func=create_function('$m','return($m);');
unset($func);
echo '. ';
}

echo 'h1OK/h1';

?

It breaks with Fatal error: Allowed memory size of 2097152 bytes 
exhausted... error. Is there any way, to solve this problem?



[2006-10-01 15:29:35] 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)






-- 
Edit this bug report at http://bugs.php.net/?id=39010edit=1



#39010 [Com]: create_function leaks memory

2009-09-21 Thread kaja dot fumei at gmail dot com
 ID:   39010
 Comment by:   kaja dot fumei at gmail dot com
 Reported By:  djgrrr at gmail dot com
 Status:   Feedback
 Bug Type: Feature/Change Request
 Operating System: *
 PHP Version:  5.1.6
 New Comment:

Just built the snapshot on my Fedora 11 machine.  No change.


Previous Comments:


[2009-09-21 17:24:11] fel...@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 16:40:10] 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-04-03 10:07:08] kexianbin at diyism dot com

You could write it like this:
isset($fn)?$fn:$fn=create_function('$v', 'echo $v;');



[2008-09-03 09:10:57] 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;$i3;$i++) {
$func=create_function('$m','return($m);');
unset($func);
echo '. ';
}

echo 'h1OK/h1';

?

It breaks with Fatal error: Allowed memory size of 2097152 bytes 
exhausted... error. Is there any way, to solve this problem?



[2006-10-01 15:29:35] 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)






-- 
Edit this bug report at http://bugs.php.net/?id=39010edit=1