Memory limit problem in php
i have a memory problem in php. I have set the limit in php.ini to 512 M
the output of /var/log/apach开发者_Go百科e2/error.log is:
PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 71 bytes) in /var/www/phpgraphlib.php on line 578,
the interesting piece of code is:
foreach ($saved_test_figurestoprint as $figuretoprint){
if (strpos($obj[$figuretoprint],",") >0 ){
$graphfilename= "graphfile".remove_invalid_chars_for_file($obj["_id"])."_".remove_invalid_chars_for_file($figuretoprint).".png" ;
$graph = new PHPGraphLib(1000,200,$graphfilename);
$data = explode(',', $obj[$figuretoprint]);
$graph->addData($data);
$graph->setTitle($figuretoprint);
$graph->setBars(false);
$graph->setLine(true);
$graph->setDataPoints(true);
$graph->setDataPointColor('maroon');
$graph->createGraph();
?> <td> <? echo $figuretoprint ; ?></td> <td> <?
echo <<<END
<imag src=$graphfilename>
END
?> </td></tr><tr><?
echo "</br></br>";
echo "used memory is ".memory_get_usage(true) . "\n";
the latest output is : used memory is 30408704
i am using a 64bit ubuntu and php 5.3. Linux mongo-db-server 2.6.35-28-generic #49-Ubuntu SMP Tue Mar 1 14:39:03 UTC 2011 x86_64 PHP Version 5.3.3-1ubuntu9.3 the problem arises if there are > 40 images to draw.
i think the new memory limit is not been applied to the server i have found this bug http://bugs.php.net/52061 but is only for memory limits > 2GB
can you help me ?
First thing I would do is make sure you are restarting your web server fully after making the php.ini change.
If that doesn't work, then try this:
Make sure you are writing to the correct php.ini file. Make a info.php file and ensure you're writing to the correct one.
At the bottom of your for loop do:
unset($graph); unset($data);
Those seem to be the largest variables and are clearly accumulating data and not being picked up by the garbage collector.
PHP unset just removes the reference to the object and it does not clears the internal object memory allocation, so on iterative process where you are creating large objects every time then do make a destroy function of your object and in that function unset each class variables used, then only unsettling the main object will free whole memory it is occupying.. hope this fact helps anybody
精彩评论