|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-07-31 13:13 UTC] chabrol at vfnm dot de
Description:
------------
If you use a SimpleXMLElement as parameter for preg_match or preg_match_all, memory is lost.
P.S.: If you cast the parameter explicit with (string)$xml->name memory usage doesn't increase. So i suppose it's an problem related to internal casting.
Reproduce code:
---------------
<?php
$xml = simplexml_load_string("<root><name>foo</name></root>");
for ($i=1; $i<=20; $i++) {
preg_match("/bar/", $xml->name);
// or preg_match_all("/bar/", $xml->name, $matches);
echo "Memory usage after $i iterations: " . memory_get_usage() . "\n";
}
?>
Expected result:
----------------
I would expect an more or less equal memory usage.
Actual result:
--------------
Memory usage is constantly increasing until hitting configured memory limit
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 13:00:01 2025 UTC |
There is no memory leak, those leaks are reported at script shutdown by the Zend Engine. to fix your problem do this preg_match("/bar/", (string)$xml->name); yes, cast it to string.Hello Judas! In my case it's an long-term running script (using php-cli). So script shutdown doesn't occur timely. Casting to string looks for me like junst an workaround because 1) other functions like ereg doesn't have the same problem. Try it with ereg("/bar/", $xml->name); instead of preg_match("/bar/", $xml->name); 2) if the engine expects an string and therefore cast the value internally, why should this behave different than casting manually in sight of memory usage? Best regards Daniel ChabrolBut if it's just a general PHP design problem, why does ereg("/bar/", $xml->name); behaves different than preg_match("/bar/", $xml->name); regarding memory-usage?