How to deal with phpQuery taking up too much memory_PHP tutorial

WBOY
Release: 2016-07-13 10:25:10
Original
693 people have browsed it

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.

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!