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

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

问题描述

我知道Javascript有一个垃圾回收器。因此,使用 delete 仅删除对该对象的引用,并且当没有对该对象的更多引用时,它将被GC删除。



Javascript很棘手,关闭,模糊名称空间和原型继承,知道何时现在或为什么并不总是显而易见的。



我正在编写一个相当大的Javascript项目,并希望避免内存泄漏,同时限制全局内存使用量。在优化阶段,我一点也没有(让我们先弄清楚这些东西:-)),但是为了避免编写糟糕的代码,了解内存管理的良好实践将会很好。




  • 所以何时应该使用 delete

  • 我应该避免的陷阱是什么,
    使用对象?

  • 关于关闭的一些知识?

  • 一些好的做法可以突出显示?

>

解决方案

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



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

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


I know that Javascript has a garbage collector. Therefor, 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 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.

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.

  • So when should I use delete ?
  • What are the traps I should avoid, using objects ?
  • Some stuff to know about closures ?
  • Some good practices to highlight ?

解决方案

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

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.

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

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

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