JavaScript memory management pitfalls?(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 内存管理的陷阱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaScript 内存管理的陷阱?


基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01