|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-08-26 06:51 UTC] apachephp at gmail dot com
Description:
------------
I'm processing big XML file (1-10MB each) in loop.
Very soon free memory is near to zero and system starts swapping.
Reproduce code:
---------------
<?
function ProcessFile($filename)
{
if (file_exists($filename)) {
$xml = simplexml_load_string(file_get_contents($filename));
foreach ($xml->report as $reports)
{
//echo $reports->forumid."\n";
}
// var_dump($xml);
unset($xml);
} else {
exit('Failed to open test.xml.');
}
}
$files=file('files.txt');
foreach ($files as $file)
{
$file=chop($file);
echo "Processing $file\n";
ProcessFile($file);
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 09:00:01 2025 UTC |
Hi! A shorter version that quickly eats up all the memory: <?php $xml = '<root><node></node></root>'; $xmldata = simplexml_load_string( $xml ); while( true ) { foreach ( $xmldata->node as $node ) { } } ?> (php5.1.5, WinXP, Shell) Also tried the latest snapshot, same effect.Hi folks: Bug report # 34112, which apparently addresses the same issue, ends with a note from a PHP developer that this is not a "bug." Bug or not, this is a problem and -- in my opinion -- should be resolved if object-oriented PHP is intended to be used, among other things, a CLI language. As one of the commenters points out, the memory depletion appears to stem from using an object inside a foreach. By the way, the following hack fixes the above code, which otherwise exhausts the allowed memory size in a split second. Yours truly, Hakan <?php $xml = '<root><node></node></root>'; $xmldata = simplexml_load_string($xml); while (true) { $foo = $xmldata->node; foreach ($foo as $node) { } } ?>