ID: 38604 Comment by: no_spam_please at obmud dot com Reported By: apachephp at gmail dot com Status: Open Bug Type: SimpleXML related Operating System: Linux PHP Version: 5.1.5 New Comment:
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) { } } ?> Previous Comments: ------------------------------------------------------------------------ [2006-09-21 16:05:16] scope at planetavent dot de 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. ------------------------------------------------------------------------ [2006-09-19 09:26:17] rolfdw at gmail dot com I dont know if this will help anyone but I found that it is the foreach that actually causes the memory leak. If you replace the FOREACH with a FOR loop it seems to be stable. ------------------------------------------------------------------------ [2006-08-28 09:48:31] apachephp at gmail dot com script: <?php $XML='<TAG1> <TAG2> <TAG3>6809586b1c38100f</TAG3> <TAG4>4323ac020</TAG4> <TAG5>NULL</TAG5> <TAG6>4323ac04e</TAG6> <TAG7>ihttp://www.mysqlperformanceblog.com/</TAG7> <TAG8>Who AM I</TAG8> <TAG9>00000232343d3062</TAG9> <TAG10>204923</TAG10> <TAG11>http://www.mysqlperformanceblog.com/</TAG11> <TAG12>http://www.mysqlperformanceblog.com/</TAG12> <TAG13 date="2006-08-22 11:44:00"> <TAG14></TAG14> <TAG15>Apachephp </TAG15> <TAG16> Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. </TAG16> <TAG17></TAG17> <TAG18></TAG18> <TAG19>0</TAG19> <TAG20></TAG20> </TAG13> <TAG21></TAG21> <TAG22></TAG22> <TAG23>2006-08-22 23:19:43</TAG23> <TAG24>Qwerrt</TAG24> <TAG25>1</TAG25> <TAG26>http://www.mysqlperformanceblog.com/</TAG26> <TAG27>http://www.mysqlperformanceblog.com/</TAG27> <TAG28>1</TAG28> <TAG29>918180</TAG29> <TAG30> MySQL Performance tuning optimization consulting</TAG30> <TAG31></TAG31> <TAG32>Simple XML </TAG32> <TAG33>0</TAG33> <TAG34>0</TAG34> <TAG35>1</TAG35> <TAG35></TAG35> <TAG36></TAG36> </TAG2> </TAG1>'; function ProcessFile($filename) { global $XML; $xml = simplexml_load_string($XML); echo "Memory: ".memory_get_usage() . "\n"; foreach ($xml->report as $reports) { //echo $reports->forumid."\n"; } // var_dump($xml); unset($xml); } while(true) { ProcessFile($file); echo "Loop ".($i++).", memory used: ".memory_get_usage() . "\n"; } ?> output: Loop 1, memory used: 57844 ... Loop 1000, memory used: 269632 .. Loop 5000, memory used: 1461740 .. Loop 10000, memory used: 2914996 Loop 65531, memory used: 17047104 Memory: 17047104 Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 3145728 bytes) in /mnt/data/home/vadim/testfeed/testxml2.php on line 57 ------------------------------------------------------------------------ [2006-08-28 08:49:54] [EMAIL PROTECTED] Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2006-08-28 08:46:08] apachephp at gmail dot com Well, I don't have XML files with 10 bytes, and with 10Mb files my memory is exhausted after processing 20 files. Memory is OK if I am processing files one per run. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/38604 -- Edit this bug report at http://bugs.php.net/?id=38604&edit=1