php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50480 memory leak with eval()
Submitted: 2009-12-15 13:27 UTC Modified: 2013-02-18 00:34 UTC
Votes:10
Avg. Score:4.4 ± 0.7
Reproduced:10 of 10 (100.0%)
Same Version:4 (40.0%)
Same OS:4 (40.0%)
From: lesjno at free dot fr Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.3.1 OS: *
Private report: No CVE-ID: None
 [2009-12-15 13:27 UTC] lesjno at free dot fr
Description:
------------
Memory used by the eval() function is not null and can be important (up to 8Ko) especially if the string is not evaluable.
This memory used can't be released. 
Used in a big loop, the memory used is growing up and I get a memory error.



Reproduce code:
---------------
$result="";
$memory_used1 = memory_get_usage();
$ch="\$result=22;"; if (@eval($ch)===false) {$result = $ch ;}
$memory_used2 = memory_get_usage();
echo "result=$result ==><b>memory difference = ".($memory_used2-$memory_used1)."<BR></b>\n";

$memory_used1 = memory_get_usage();
$ch="abcdef"; if (@eval($ch)===false) {$result = $ch ;}
$memory_used2 = memory_get_usage();
echo "result=$result ==><b>memory difference = ".($memory_used2-$memory_used1)."<BR></b>\n";

 

Expected result:
----------------
result=22 ==>memory difference = 0
result=abcdef ==>memory difference = 0



Actual result:
--------------
result=22 ==>memory difference = 200
result=abcdef ==>memory difference = 7480



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-16 12:47 UTC] jani@php.net
Please provide such script that actually has a loop. Simple test suggested this is just expected and memory does not really grow after certain point.
 [2009-12-22 08:59 UTC] lesjno at free dot fr
Hello jani,
I have a loop to update a database from an excel file. So, for each excel row I will apply several tests and initializations specified in a configuration file. The configuration file depend on the user profile. The eval() function is used, for example, for data initialization : the configuration file can give a data or a code. For example a specific date field can take a fix date ("2009-01-01") or can be computed. So, for example, I can found in a configuration file :
for a user category : $field_init = '2009-01-01' ;
for another user category : $field_init = 'return date("Y-m-d");' ;
I will use the following code :
$data = @eval($field_init) ; if ($data===false) $data=$field_init;
It's only an example but there are a lot of others "eval()" like this for each excel row treatment.
An excel file can have more than 50000 rows ... so the memory leak is growing up till the system error ...
 [2009-12-24 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".
 [2010-04-25 20:39 UTC] felipe@php.net
-Status: No Feedback +Status: Feedback
 [2010-04-25 20:39 UTC] felipe@php.net
Please try using this snapshot:

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

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


 [2010-04-26 10:09 UTC] lesjno at free dot fr
-Status: Feedback +Status: Open
 [2010-04-26 10:09 UTC] lesjno at free dot fr
I've just tried the PHP 5.3.2 release but the memory leak remains.
The test script is the following :
CODE :
------
$status="";
$ch="\$result=true;";  // a chain with a good php syntax
$memory_used1 = memory_get_usage();
$status = @eval($ch) ;
$memory_used2 = memory_get_usage();
echo "chain to eval : \"$ch\" ==> status = ";
var_dump($status);
echo "<BR>";
echo "<b>memory difference = ".($memory_used2-$memory_used1)."<BR><BR></b>\n";

$ch="abcdef";   // a chain with a bad php syntaxe
$memory_used1 = memory_get_usage();
$status = @eval($ch) ;
$memory_used2 = memory_get_usage();
echo "chain to eval : \"$ch\" ==> status = ";
var_dump($status);
echo "<BR>";
echo "<b>memory difference = ".($memory_used2-$memory_used1)."<BR></b>\n";

Actual result:
--------------
chain to eval : "$result=true;" ==> status = NULL
memory difference = 512

chain to eval : "abcdef" ==> status = bool(false)
memory difference = 8632


Expected result:
--------------
chain to eval : "$result=true;" ==> status = NULL
memory difference = 0

chain to eval : "abcdef" ==> status = bool(false)
memory difference = 0
 [2010-05-27 13:26 UTC] letssurf at gmail dot com
PHP Versions:
-------------
Tested as broken with
5.2.6
5.3.2

Reproduce code:
---------------

<?php
// working code, runs for ever
$code = 'return false;';
while(true) {
   $result = eval($code);
   $mem = memory_get_peak_usage(true);
   var_dump($result, $mem);
}

<?php
// broken code, dies due to memory limit being reached
$code = 'some invalid php code';
while(true) {
   $result = eval($code);
   $mem = memory_get_peak_usage(true);
   var_dump($result, $mem);
}


Expected result:
----------------
A var_dump of false and the peak memory usage, not increasing.


Actual result:
--------------
A var_dump of false and the peak memory usage keeps increasing.
 [2011-09-12 19:54 UTC] horsarias at hotmail dot com
Hi, my php console process reaches the memory limit after some days of hard work but the only suspect that i find in de code is the "eval". using linux with php 5.3.6  ( in my code, the eval function is HEAVALY usted with HEAVY arrays being evaluated)

Bye
 [2011-11-16 13:58 UTC] felipe@php.net
-Status: Open +Status: Feedback
 [2011-11-16 13:58 UTC] felipe@php.net
Please try using this snapshot:

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

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


 [2013-02-18 00:34 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 [2015-11-17 14:10 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: Sat Dec 21 14:01:32 2024 UTC