XPath を使用して HTML を分析するコマンド ライン php スクレイピング アプリを構築しました。問題は、新しい DOMXPath クラス インスタンスがループで読み込まれるたびに、読み込まれる XML のサイズとほぼ同じメモリ損失が発生することです。 . スクリプトが実行され、実行され、制限に達して終了するまでメモリ使用量がゆっくりと増加します。
でガベージ コレクションを強制しようとしましたがgc_collect_cycles()
、PHP はまだ古い Xpath 要求からメモリを取得していません。実際、DOMXPath クラスの定義には、デストラクタ関数さえ含まれていないように見えますか?
So my question is ... is there any way to force garbage clean up on DOMXPath
after I've already extracted the necessary data? Using unset on the class instance predictably does nothing.
The code is nothing special, just standard Xpath stuff:
//Loaded outside of loop
$this->dom = new DOMDocument();
//Inside Loop
$this->dom->loadHTML($output);
$xpath = new DOMXPath($this->dom);
$nodes = $xpath->query("//span[@class='ckass']");
//unset($this->dom) and unset($xpath) doesn't seem to have any effect
As you can see above I've kept the instantiation of a new DOMDocument
class outside of the loop, although that doesn't seem to improve performance. I've even tried taking the $xpath
class instance out of the loop and loading the DOM into Xpath directly using the __constructor
method, memory loss is the same.