simple_html_dom的PHP内存泄漏(在'parse()'函数之后)

时间:2013-11-07 08:18:02

标签: php parsing memory-leaks simple-html-dom

我使用的是simple_html_dom。

我发现在"parse"函数中运行的"load"函数会导致内存泄漏,而这似乎无法找到原因。

我在a answered by Flash Thunder尝试了"$html->clear""unset($html)""clean_all"

经过所有的工作,记忆仍然没有清除。

我的结果在这里: http://i.stack.imgur.com/CYFeS.jpg

所以,我想知道如何清除幽灵记忆?

这是我的测试代码:

<? 
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);
            }
        }
    }
}
?>

另外,我在simple_html_dom.php中添加了一些代码(echo memory_get_usage)。 经过几次尝试,我发现事情发生在“加载”功能(在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();

}

1 个答案:

答案 0 :(得分:0)

你没有正确清除dom对象,你忘记了括号:

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

以下是更正后代码的demo

这是输出:

此外,如果您有任何检测到的错误或可能的补丁,请随时将其报告给their bugs/patch tracker