Phpquery How to handle excessive memory consumption

Source: Internet
Author: User
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.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.