JavaScript对象的生命和内存泄漏 [英] Life of JavaScript objects & Memory Leaks

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

问题描述

我已经对此进行了相当多的研究,但主要是将其他问题拼凑在一起,这仍然存在一些疑问。在一个不会随时刷新浏览器页面的应用程序中,可能会在不关闭的情况下生存很长时间(小时)(假设刷新页面或导航到另一个页面会重新启动js代码),确保对象的最佳方法是什么释放并且没有内存泄漏。

I have researched quite a bit about this but mostly by piecing other questions together, which still leaves some doubt. In an app that does not refresh the browser page at any time and may live for quite a while (hours) without closing (assuming that refreshing a page or navigating to another would restart the js code), what's the best way to ensure objects are released and that there's no memory leak.

这些是我关注的特定情况:

These are the specific scenarios I'm concerned about:

全部以下代码在一个揭示模块模式内。

All of the code below is within a revealing module pattern.

mycode = function(){}()

函数中的变量,我确信GC收集的这个很好

variables within functions, I'm sure this one is collected by the GC just fine

function(){ var h = "ss";}


$ b模块中的$ b

变量,当不再需要时,g = null?

variables within the module, should g = null when it's no longer needed?

var g;
function(){ g = "dd";}

最后是a的生命jqXHR:它返回后清理了吗?它是否应该在所有情况下都设置为null作为预防措施是否保留在函数或模块中?

And lastly the life of a jqXHR: is it cleaned up after it returns? Should it be set to null in all cases as a precaution whether kept inside a function or module?

如果这样做,它是否在GC返回后被x清理?:

If doing this, is it x cleaned up by the GC after it returns?:

function(){
   var x = $.get();
   x.done = ...;
   x.fail = ...;
}

执行此操作时,x返回后是否也会清理它? :

How about when doing this, will it also be cleaned up after x returns?:

var x;
function(){
   x = $.get();
   x.done = ...;
   x.fail = ...;
}

最后,有没有办法清理所有变量并重新启动模块而不重新启动浏览器?

Lastly, is there a way to cleanup all variables and restart a module without restarting the browser?

推荐答案


函数中的变量,我确信这是由GC收集的很好

variables within functions, I'm sure this one is collected by the GC just fine

是。


变量在模块中,当不再需要时,g = null吗?

variables within the module, should g = null when it's no longer needed?

当然。


最后jqXHR的生命周期是:它在返回后被清理干净了吗?是否应该在所有情况下将其设置为null作为预防措施是否保留在函数或模块中?

And lastly the life of a jqXHR: is it cleaned up after it returns? Should it be set to null in all cases as a precaution whether kept inside a function or module?

各种浏览器都有与之相关的错误导致 onreadystatechange 的XHR以及它关闭的任何东西都无法收集,除非开发人员小心地用虚拟值替换它( xhr.onreadystatechange = new函数(''))但我相信jQuery会为你处理这个问题。

Various browsers have had bugs related to XHR that caused the onreadystatechange and anything it closed over to remain uncollectable unless the dev was careful to replace it with a dummy value (xhr.onreadystatechange = new Function('')) but I believe jQuery handles this for you.


最后,有没有办法清理所有变量并重新启动模块而不重新启动浏览器?

Lastly, is there a way to cleanup all variables and restart a module without restarting the browser?

与页面关联的全局状态将占用浏览器内存,直到页面从浏览器历史记录堆栈中逐出。 location.replace 可以通过让你杀死当前页面并将其替换为相同应用程序的新版本而不扩展历史堆栈来帮助你。

Global state associated with the page will take up browser memory until the page is evicted from the browser history stack. location.replace can help you here by letting you kill the current page and replace it with a new version of the same app without expanding the history stack.


替换当前文档与提供的URL处的文档。与 assign()方法的区别在于使用 replace()后,当前页面将不会保存在会话中历史,意味着用户将无法使用后退按钮导航到它。

Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

当您使用模块一词时,这不是一个对浏览器或其JavaScript解释器具有明确定义的术语,因此无法从内存中驱逐模块和模块。您可能需要担心的一些事情可能会将内容保留在内存中:

When you use the word "module", that is not a term that has a well-defined meaning to the browser or its JavaScript interpreter so there is no way to evict a module and only a module from memory. There are several things that you have to worry about that might keep things in memory:


  1. 对已添加到DOM节点的JavaScript对象的引用它们关闭的一切 - 事件处理程序是一个非常常见的例子。

  2. 实时 setInterval setTimeout 回调及其​​关闭的所有内容。

  3. 全局对象的属性及其关闭的所有内容。

  4. 如您所述,某些主机对象的属性,如XHR实例,Web工作者回调等,以及(您猜对了)它们关闭的所有内容。

  1. References to JavaScript objects that have been attached to DOM nodes and everything they close over -- event handlers are a very common example.
  2. Live setInterval and setTimeout callbacks and everything they close over.
  3. Properties of the global object and everything they close over.
  4. As you noted, properties of certain host objects like XHR instances, web worker callbacks, etc. and (you guessed it) everything they close over.

任何要卸载模块的方案,只有一个模块需要处理所有这些,并找出哪些是模块的一部分,哪些不是。这是很多不同类型的清理工作。

Any scheme that is going to unload a module and only a module would need to deal with all of these and figure out which of them are part of the module and which are not. That's a lot of different kinds of cleanup.

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

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