JavaScript 内存管理的陷阱? [英] JavaScript memory management pitfalls?

查看:20
本文介绍了JavaScript 内存管理的陷阱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 JavaScript 有一个垃圾收集器.因此,使用delete只删除一个对该对象的引用,当没有对该对象的引用时,它被GC删除.

I know that JavaScript has a garbage collector. Therefore, using delete remove only a reference to the object, and when there is no more reference to this object, it is deleted by the GC.

JavaScript 很棘手,由于闭包、模糊的命名空间和原型继承,知道什么时候到现在或为什么并不总是很明显.

JavaScript is tricky, with the closures, the fuzzy name space and the prototype inheritance, it's not always obvious to know when to now or why.

我正在编写一个相当大的 JavaScript 项目,并希望避免内存泄漏,同时限制全局内存使用.我根本不在优化阶段(让我们先让这些东西工作:-)),但如果知道内存管理的良好做法以避免编写糟糕的代码,那就太好了.

I am coding a fairly large JavaScript project and would like to avoid memory leak, while limiting the global memory usage. I am not at all in the optimizing stage (let's get the stuff work first :-)), but it would be nice to know the good practices for memory management in order to avoid writing crappy code.

  • 那么我应该什么时候使用delete?
  • 我应该避免哪些陷阱,使用对象?
  • 关于闭包的一些知识?
  • 要强调一些好的做法?

推荐答案

根据我的经验,垃圾收集器的实现好/不好取决于浏览器.应用良好的面向对象编程实践是一个好的开始.

From my experience, Garbage Collectors are well/poorly implemented depending on the browser. Applying good Object Oriented programming practices is a good start.

我唯一的建议:不要通过连接 DOM 和javascript 对象(不会被 DOM 和 JS GC 清除的循环引用).这些错误会比您在应用程序中实例化的任何对象消耗更多的内存.

My only advice: do not create memory leaks by connecting DOM & javascript objects (circular references that won't be cleared by DOM and JS GCs). These mistakes will eat far more memory than any object you will instantiate within your application.

有关 DOM/JS 内存泄漏的更多详细信息.http://msdn.microsoft.com/en-us/library/bb250448%28VS.85%29.aspx

More details on DOM/JS memory leaks. http://msdn.microsoft.com/en-us/library/bb250448%28VS.85%29.aspx

这篇关于JavaScript 内存管理的陷阱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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