Phpquery How to handle excessive memory consumption
Resolves an issue in which phpquery consumes too much memory
Phpquery is a jquery-like open source project implemented in PHP that parses web page elements in the form of jquery in the server-side. Relative hermetical or other way to match the way of web pages, phpquery to use more convenient.
When using Phpquery to capture a webpage, there is a problem: after processing a large number of pages, Phpquery occupies a staggering amount of memory (faster than 1G),
For example, this code:
while (true) { phpquery::newdocumentfile ($htmlFile); Working with page elements ... Echo Memory_get_usage (). "\ n";}
Run the above code with caution, and it will soon be running out of your memory.
After viewing the source code of Phpquery finally found the problem, phpquery in each processing a page will produce a Domdocumentwrapper object, and each Domdocumentwrapper object will be saved in the static member $ Documents (in Phpquery::createdocumentwrapper), this variable is an array, and each parsing of a page array element is incremented by one.
Phpquery:: $documents [$wrapper->id] = $wrapper;
Find the problem, the solution is very easy, each parsing a page, the Phpquery:: $documents empty.
while (true) { phpquery::newdocumentfile ($htmlFile); Working with page elements ... Phpquery:: $documents = Array (); Echo Memory_get_usage (). "\ n";}
The memory footprint is stable.