phpQuery is an open source project similar to jQuery implemented in php, which can parse web page elements in jQuery syntax on the server side. Compared with regular or other methods of matching web pages, phpQuery is much more convenient to use.
When using phpQuery to collect web pages, I encountered a problem: after processing a large number of web pages, phpQuery occupied an amazing amount of memory (soon exceeding 1G).
For example, this code:
Copy code The code is as follows:
while (true) {
phpQuery::newDocumentFile($htmlFile);
// Processing Web element...
echo memory_get_usage() . "n";
}
Run the above code with caution, it will use up your memory quickly.
After checking the source code of phpQuery, I finally discovered the problem. phpQuery will generate a DOMDocumentWrapper object every time it processes a web page, and each DOMDocumentWrapper object will be saved in the static member $documents (phpQuery::createDocumentWrapper). This variable is an array, which is increased by one each time a web page array element is parsed.
phpQuery::$documents[$wrapper->id] = $wrapper;
After you find the problem, it is easy to solve it. Every time you parse a web page, just leave phpQuery::$documents blank. .
Copy code The code is as follows:
while (true) {
phpQuery::newDocumentFile($htmlFile);
// Process web page elements...
phpQuery::$documents = array();
echo memory_get_usage() . "n";
}
Memory usage Stable.
http://www.bkjia.com/PHPjc/825183.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825183.htmlTechArticlephpQuery is an open source project similar to jQuery implemented in php. It can parse web pages in jQuery syntax on the server side. element. Compared with regular or other ways to match web pages, p...