简体   繁体   中英

PHP memory leak of simple_html_dom (after the 'parse()' function)

I am using simple_html_dom.

I have found the "parse" function, which runs in the "load" function, will cause a memory leak that can't seem to find why.

I have try "$html->clear" , "unset($html)" , also "clean_all" from a answered by Flash Thunder .

after all thoese work, the memory is still no clear up.

my result here : http://i.stack.imgur.com/CYFeS.jpg

So, I like to know how can I do to clear that ghost memory ?

here is my testing code :

<? 
include('include/simple_html_dom.php');
ini_set('memory_limit', '128M');

echo 'memory =',round(memory_get_usage()/1000),' KB<br>';  
$url = 'http://www.marketwatch.com/';
$html = file_get_html($url);

foreach($html->find('div#mostpopular img') as $images){
    if($images->src !='')echo $images-> outertext ;
}

echo '<br>';

echo 'memory before clear =',round(memory_get_usage()/1000),' KB<br>';  
$html->clear;
$images->clear;

unset($html);
unset($images);

echo 'memory after clear =',round(memory_get_usage()/1000),' KB<br>'; 

echo 'memory before clean_all =',round(memory_get_usage()/1000),' KB<br>';
clean_all($GLOBALS);

echo 'memory after clean_all =',round(memory_get_usage()/1000),' KB<br>'; 



function clean_all(&$items,$leave = ''){
    foreach($items as $id => $item){
        if($leave && ((!is_array($leave) && $id == $leave) || (is_array($leave) && in_array($id,$leave)))) continue;
        if($id != 'GLOBALS'){
            if(is_object($item) && ((get_class($item) == 'simple_html_dom') || (get_class($item) == 'simple_html_dom_node'))){
                $items[$id]->clear();
                unset($items[$id]);
            }else if(is_array($item)){
                $first = array_shift($item);
                if(is_object($first) && ((get_class($first) == 'simple_html_dom') || (get_class($first) == 'simple_html_dom_node'))){
                    unset($items[$id]);
                }
                unset($first);
            }
        }
    }
}
?>

Also, I put some code (echo memory_get_usage) in simple_html_dom.php. After a few of try, I found the things happens in "load" function (somewhere around line 818) :

// load html from string
function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT) {
    global $debugObject;
    // prepare
    $this->prepare($str, $lowercase, $stripRN, $defaultBRText);
    // strip out comments
    $this->remove_noise("'<!--(.*?)-->'is");
    // strip out cdata
    $this->remove_noise("'<!\[CDATA\[(.*?)\]\]>'is", true);

    // Per sourceforge http://sourceforge.net/tracker/?func=detail&aid=2949097&group_id=218559&atid=1044037
    // Script tags removal now preceeds style tag removal.
    // strip out <script> tags
    $this->remove_noise("'<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>'is");
    $this->remove_noise("'<\s*script\s*>(.*?)<\s*/\s*script\s*>'is");
    // strip out <style> tags
    $this->remove_noise("'<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>'is");
    $this->remove_noise("'<\s*style\s*>(.*?)<\s*/\s*style\s*>'is");
    // strip out preformatted tags
    $this->remove_noise("'<\s*(?:code)[^>]*>(.*?)<\s*/\s*(?:code)\s*>'is");
    // strip out server side scripts
    $this->remove_noise("'(<\?)(.*?)(\?>)'s", true);
    // strip smarty scripts
    $this->remove_noise("'(\{\w)(.*?)(\})'s", true);

    // parsing
    echo 'memory before parsing() =',round(memory_get_usage()/1000),' KB<br>';  
    while ($this->parse());
    echo 'memory after parsing()  =',round(memory_get_usage()/1000),' KB<br>';   
    // end          
    $this->root->_[HDOM_INFO_END] = $this->cursor;
    $this->parse_charset();

}

You're not clearing the dom objects correctly, you forgot the parentheses:

$html->clear();
$images->clear();

Here's a demo of the corrected code

And here's the ouput:

Also, if you have any detected bug or possible patch, feel free to report it to their bugs/patch tracker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM