简体   繁体   English

避免浪费内存

[英]Avoiding wasteful memory consumption

Using php.exe 5.2.17.17 on Windows 7, this: 在Windows 7上使用php.exe 5.2.17.17:

include_once('simple_html_dom.php');

function fix($setlink)
{
    $setaddr = $setlink->href;
    $filename="..\\".urldecode($setaddr);
    $set=file_get_contents($filename);  
    $setstr = str_get_html($set);
    // Do stuff requiring whole file
    unset($set);
    unset($setstr);
}

$setindexpath = "..\index.htm";
foreach(file_get_html($setindexpath)->find('a.setlink') as $setlink)
{
    fix($setlink);  
}

(relying on external data files) fails thus: (依靠外部数据文件)失败,因此:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in [snip]\simple_html_dom.php on line 620

"function fix" is a suggestion from the answer to a similar question here. “功能修复”是对此处类似问题的答案的建议。 unset() is wishful thinking :-) unset()是一厢情愿的想法:-)

How may I avoid the continuing consumption of memory by the strings unused on the next loop iteration? 如何避免下一次循环迭代中未使用的字符串持续消耗内存? Without defacing the code too much. 无需过多破坏代码。 And while providing the whole file as string. 并且在将整个文件提供为字符串的同时。

try $setstr->clear(); 尝试$setstr->clear(); before unset($setstr); 在未unset($setstr);之前unset($setstr);
see http://simplehtmldom.sourceforge.net/manual_faq.htm#memory_leak 参见http://simplehtmldom.sourceforge.net/manual_faq.htm#memory_leak

side note: $setstr seems to be a misnomer; 旁注:$ setstr似乎用词不当; it's not a string but the dom repesentation of the html doc. 它不是字符串,而是html文档的dom代表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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