简体   繁体   English

如何防止IE Mobile中的内存泄漏?

[英]How Can I Prevent Memory Leaks in IE Mobile?

I've written an application for use offline (with Google Gears) on devices using IE Mobile. 我已经编写了一个应用程序,该应用程序可在使用IE Mobile的设备上离线使用(与Google Gears一起使用)。 The devices are experiencing memory leaks at such a rate that the device becomes unusable over time. 设备正在经历内存泄漏,其速率使设备随着时间的推移变得无法使用。

The problem page fetches entries from the local Gears database and renders a table of each entry with a link in the last column of each row to open the entry ( the link is just onclick="open('myID')" ). 问题页面从本地Gears数据库中获取条目,并呈现每个条目的表,并在每行的最后一列中提供一个链接以打开该条目(该链接只是onclick =“ open('myID')”)。 When they've done with the entry they return to the table, which is RE-rendered. 完成输入后,它们将返回到表,该表将重新呈现。 It's the repeated building of this table that appears to be the problem. 问题是表的重复构建。 Mainly the onclick events. 主要是onclick事件。

The table is generated in essence like this: 该表实质上是这样生成的:

var tmp="";
for (var i=0; i<100; i++){
 tmp+="<tr><td>row "+i+"</td><td><a href=\"#\" id=\"LINK-"+i+"\""+
  " onclick=\"afunction();return false;\">link</a></td></tr>";
}

document.getElementById('view').innerHTML = "<table>"+tmp+"</table>";

I've read up on common causes of memory leaks and tried setting the onclick event for each link to "null" before re-rendering the table but it still seems to leak. 我已经阅读了导致内存泄漏的常见原因,并尝试在重新呈现表之前将每个链接的onclick事件设置为“ null”,但它似乎仍然泄漏。

Anybody got any ideas? 有人有想法吗?

In case it matters, the function being called from each link looks like this: 万一重要,从每个链接调用的函数如下所示:

function afunction(){
 document.getElementById('view').style.display="none";
}

Would that constitute a circular reference in any way? 它将以任何方式构成通函吗?

Jake 杰克

I don't know if it will help with memory, but instead of concatenating your strings, you can push them onto an array and join them for better performance. 我不知道它是否对内存有帮助,但是您可以将它们压入数组中并加入它们以提高性能,而不必串联字符串。 Something like: 就像是:

var tmp=[];
for (var i=0; i<100; i++){
 tmp.push("<tr><td>row "+i+"</td><td><a href=\"#\" id=\"LINK-"+i+"\""+
  " onclick=\"afunction();return false;\">link</a></td></tr>");
}

document.getElementById('view').innerHTML = "<table>"+tmp.join('')+"</table>";

If you are doing a lot of scripting that changes the content of the page there is an IE memory leak that was mentioned in an old AJAX book I have used in the past (and it may well be hitting your here). 如果您正在做很多脚本更改页面内容,那么在我过去使用过的一本旧AJAX书中提到了IE内存泄漏(很可能会在这里遇到问题)。 When a page unload, you need to clear any divs/spans/etc that you have changed, or their contents will remain in memory. 卸载页面时,您需要清除所有已更改的div / spans / etc,否则它们的内容将保留在内存中。 Try something like 尝试类似

<script type="text/javascript">
window.onunload = clearAJAXContent;

function clearAJAXContent() {
/* clear any dynamic content placeholders here*/
}
</script>

I believe, when clearing, you want to set the innerHTML to "". 我相信,在清除时,您希望将innerHTML设置为“”。 When I get home next I'll try to find the book and update my answer if there's anything different, but that should give you something to test in the meantime 接下来我回到家时,如果有任何不同之处,我将尝试查找该书并更新我的答案,但这同时应该为您提供一些测试

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

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