I'm using PHPQuery (HTML parser) in a loop, and unsetting the previous document each time by using phpQuery::unloadDocuments()
. The loop simplifies to:
while(...){
$doc="parsed stuff";
...
unset($doc);
}
However, there's memory leak in this because the script runs out of memory after a while. After searching online, someone recommended that to avoid memory leak, I should wrap it in a function, so I changed it to this:
function r(){
$doc="parsed stuff";
...
unset($doc);
}
while(...)
r();
Now, the memory leak stopped. Why is this?
See this http://php.net/manual/en/language.references.unset.php, is has good example there.