javascript、循环引用和内存泄漏 [英] javascript, circular references and memory leaks

查看:20
本文介绍了javascript、循环引用和内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得不久以前,Javascript 解释器在面对循环引用时会遇到内存泄漏问题.

From what I recall of a not too distant past, Javascript interpreters suffered from memory leaking issues when faced with circular references.

在最新的浏览器中是否仍然如此?(例如 Chrome、FF 3.5 等)

Is it still the case in the latest browsers? (e.g. Chrome, FF 3.5 etc)

推荐答案

当您在 JavaScript 对象和宿主对象(如 DOM 节点)之间进行引用循环时,我们谈论的绝大多数 JavaScript 泄漏都特别出现在 IE6-7 中.

The vast majority of leaks we talk about with JavaScript are specifically in IE6-7 when you make a reference loop between JavaScript objects and host objects like DOM nodes.

在 IE6 中,这是特别有害的,因为当您离开页面时无法取回内存;它会消失,直到您退出浏览器.在 IE7 中,清除页面现在确实会返回内存,但是当您有一个长时间运行的应用程序时,您仍然会遇到困难.IE8 通过将 DOM 节点转换为原生 JavaScript 对象而不是宿主对象,正确解决了大部分问题.(您仍然可以通过在引用循环中包含其他非本地对象(如 ActiveX 对象)来触发 IE8 中的泄漏.)

In IE6 this is particularly pernicious in that you don't get the memory back when you leave the page; it's gone until you quit the browser. In IE7 clearing out the page does now return the memory, but you can still have difficulty when you have a long-running application. IE8 solves most of this problem properly by turning the DOM nodes into native JavaScript objects instead of host objects. (You could still trigger the leaks in IE8 by including other non-native objects like ActiveX objects in a reference loop.)

对于所有浏览器,尤其是在旧版本中,肯定仍然会在随机的地方潜伏着小的隐秘的内存泄漏.但是没有一种方法可以像 IE refloop 问题一样轻松地分类和避免它们.

There will certainly still be small obscure memory leaks lurking around in random places for all the browsers, especially in older versions. But there's no one way to easily categorise and avoid them like with the IE refloop issue.

这篇关于javascript、循环引用和内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆