javascript - 运行 javascript 过夜后系统挂起

标签 javascript jquery html memory-management memory-leaks

我的 javascript 非常简单。它只是不断调用 ajax 来下载数据并将其显示在我的屏幕上。我通过直接更新数据(使用 jquery 的 $(DIV).empty() 函数然后附加到另一个内容)来重用那些 DOM 元素。然而,在运行了一夜之后,我的电脑挂了,似乎还在做一些事情。

我试着观察一些 firefox 插件的内存消耗。运行两个小时,内存可能会增长200MB,但我不确定是我的js还是其他原因导致的。

我对这个问题有些疑问。 1.有没有什么好的监控方法(比如具体网页占用了多少内存,占用了多少CPU资源,怎么样?) 2. 是否有一些通用的方法来设计 js web 应用程序来避免此类问题?

谢谢,欢迎任何评论!

最佳答案

某些版本的 Firefox 使用引用计数作为垃圾收集策略。引用计数在某些情况下容易造成内存泄漏。

Internet Explorer and Mozilla Firefox are the two Web browsers most commonly associated with memory leaks in JavaScript. The culprit in both browsers is the component object model used to manage DOM objects. Both the native Windows COM and Mozilla's XPCOM use reference-counting garbage collection for memory allocation and retrieval. Reference counting is not always compatible with the mark-and-sweep garbage collection used for JavaScript. This article focuses on ways to work around memory leaks in JavaScript code. See Resources to learn more about COM layer memory handling in Firefox and IE.

来自 http://www.ibm.com/developerworks/web/library/wa-memleak/

关于javascript - 运行 javascript 过夜后系统挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11303255/

相关文章:

javascript - 正则表达式效率,在 html 中选择文本的更好方法

javascript - 如何单击输入字段内的字形图标

c# - 提交按钮是否在表单提交时将其值传递给操作方法?

html - HTML5 中的 Web Speech API 支持哪些语言?

javascript - Express JS 部分未加载样式表

javascript - 有没有办法隐藏 Javascript 注释?

javascript - 1次点击打开2个链接

javascript - 使用 jquery animate() 在文本上实现反弹效果?

html - 无法设置 overflow-x visible 和 overflow-y auto

javascript - 向列表项添加 anchor 标记会使列表项消失?