简体   繁体   English

JavaScript中的内存泄漏(Chrome)

[英]Memory leak in JavaScript (Chrome)

I'm calling a function 50 times a second, which does some expensive things as it is painting alot on a <canvas> element. 我正在调用一个50次的函数,这会做一些昂贵的事情,因为它在<canvas>元素上绘制很多。

It works great, no problems there, but I just took a look at the memory usage and it was stealing 1MB a second of my RAM. 它工作得很好,没有任何问题,但我只看了一下内存使用情况,它每秒内存占用1MB。 Chrome seems to garbage collect, as it went down each minute or so, but then the usage grew again. Chrome似乎是垃圾收集,因为它每分钟都会消失,但随后用量再次增长。

What I tried is putting return at certain places in my function so as to decide what part of my function exactly causes the leak. 我尝试的是在我的函数中的某些位置return ,以便确定我的函数的哪个部分确切地导致泄漏。 I've been able to cut it down to a specific line of code, after which the evil part comes, but I don't really know how to solve it. 我已经能够将其缩减为特定的代码行,之后邪恶的部分来了,但我真的不知道如何解决它。

My questions are: 我的问题是:

  • What tool is available to effectively measure JavaScript memory leaks in Chrome? 有哪些工具可用于有效衡量Chrome中的JavaScript内存泄漏?
  • Would it be effective to set variables to null / undefined after they have been used, something like disposing them? 在使用变量后将变量设置为null / undefined会有效吗,比如处理它们?

If the source code is really necessary I wouldn't hestitate to post it here, but I must admit that it's both long and perhaps a little ununderstandable for others. 如果源代码确实是必要的,我会毫不犹豫地在这里发布它,但我必须承认它既长又可能对其他人来说有点不可理解。

I'm just going to pull this quote directly, linked from the article; 我只是直接引用这句话,从文章中链接;

Speaking of memory leaks, breaking circular references — the cause of the leaks — is usually done with simple null assignment. 说到内存泄漏,通常使用简单的null赋值来打破循环引用 - 泄漏的原因。 There's usually no need to use delete. 通常不需要使用删除。 Moreover, null'ing allows to “dereference” variables — what delete would normally not be able to do. 此外,null'ing允许“取消引用”变量 - 删除通常无法执行的操作。

var el = document.getElementById('foo');
// circular reference is formed
el.onclick = function() { /* ... */ };
// circular reference is broken
el = null;
// can't `delete el` in this case, as `el` has DontDelete

For these reasons, it's best to stick with null'ing when breaking circular references. 由于这些原因,最好在打破循环引用时坚持使用null。

delete Explained 删除说明

Look at heap profile under the Profiles tab in Chrome's developer tools for information about memory usage. 查看Chrome开发人员工具中“配置文件”选项卡下的堆配置文件,了解有关内存使用情况的信息。

You can do the following to prevent memory leaks: 您可以执行以下操作以防止内存泄漏:

  • Test your code with JSLint , to see if that will give you some pointers. 使用JSLint测试代码,看看是否会给你一些指示。
  • Use the var keyword to give your variables function scope, so they can be garbage collected when they go out of scope. 使用var关键字为变量提供函数范围,因此当它们超出范围时可以进行垃圾收集。 Without the var keyword variables have global scope. 没有var关键字变量具有全局范围。
  • Use delete variable; 使用delete variable; statements to remove the object as well as the reference from memory. 用于从内存中删除对象和引用的语句。 Setting the variable to null will only remove the object from memory, but not its reference. 将变量设置为null只会从内存中删除对象,但不会删除其引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM