Does this cause a PHP memory leak?
function xml_parse($xmlString)
{
return json_decode(json_encode(new SimpleXMLElement($xmlString)), true);
}
Or do i need to unset the SimpleXML obj like this?
function xml_parse($xmlString)
{
$sObj = new SimpleXMLElement($xmlString);
$ret = json_decode(json_encode($sObj), true);
unset($sObj);
return $ret;
}
I am running this in a large foreach as the script needs to parse lots of files.
If you really want to know, run it a couple thousand times and output the memory usage. Normally, unused objects will be cleaned up by the garbage collector, so the first snippet wouldn’t ‘leak’ memory. However, in PHP <5.3 there is an issue with circular references which can prevent unused objects from being collected. So if you do experience an issue, updating PHP might solve it for you.