简体   繁体   English

Javascript:命名关闭和内存管理

[英]Javascript: named closure and memory management

I'm trying to know more about memory management in JS and I have some question about closures. 我试图了解有关JS中的内存管理的更多信息,而我对闭包有一些疑问。

Case 1 : 情况1

// Suppose that object var is capable to emit events 
var object = new EventEmitter();

object.addEventListener('custom-event', function callback(event) {
    object.removeEventListener('custom-event', callback);
    object = null;

    //Do some heavy computation like opening a specific view or somehthing similar
    var heavy_window = new HeavyWindow();
    heavy_window.open();
});

Case 2 : 情况2

// Suppose that object var is capable to emit events 
var object = new EventEmitter();

object.addEventListener('custom-event', callback = function(event) {
    object.removeEventListener('custom-event', callback);
    object = null;

    //Do some heavy computation like opening a specific view or somehthing similar
});

My questions are: 我的问题是:

  • Case 1: 情况1:

    • Is correct to think that the object remains in memory until the heavy_window is not nulled? 以为object一直保留在内存中,直到heavy_window没有为空是正确的吗?
    • Nulling the object var inside the closure can help gc? 将闭包内的object var清空可以帮助gc吗?
  • Case 2: 情况2:

    • Naming a closure in this way ...addEventListener(callback = function() {}) instead of ...addEventListener(function callback() {}) can cause a memory leaks? 以这种方式命名闭包...addEventListener(callback = function() {})而不是...addEventListener(function callback() {})会导致内存泄漏吗? Declaring callback = function() {} will cause a global hidden variable? 声明callback = function() {}是否会导致全局隐藏变量?

Note, I don't need examples in jQuery or using other framework. 注意,我不需要jQuery或其他框架的示例。 I'm interested to know more in JavaScript vanilla. 我有兴趣进一步了解JavaScript Vanilla。

Thank you in advance. 先感谢您。

I don't think the details for garbage collecting is specified in the ES specification. 我认为ES规范中没有指定垃圾收集的详细信息。 However in most interpreters, objects will usually be garbage-collected once there doesn't exist any references to the object or not accessible, or scheduled at a later time. 但是,在大多数解释器中,一旦不存在对对象的任何引用或无法访问或在以后计划该对象,通常将对其进行垃圾回收。

In case 1, heavy_window might have access to the original new EventEmitter ( event often has references to the event target) if you pass the event object into HeavyWindow , and thus your new EventEmitter() might not be garbage-collected until all references to your heavy_window are gone. 在情况1中,如果将事件对象传递给HeavyWindow ,那么heavy_window可能可以访问原始的new EventEmitterevent通常具有对事件目标的引用),因此,直到对您的所有引用进行引用之后,您的new EventEmitter()才可能被垃圾回收。 heavy_window不见了。 If not, then your new EventEmitter will be garbage-collected at some point in the future. 如果没有,那么您的new EventEmitter将在将来的某个时刻被垃圾回收。

In case 2, assigning the callback function to a global variable callback will not change anything because the callback function do not have any reference to the actual new EventEmitter object you have created. 在情况2中,将回调函数分配给全局变量callback不会发生任何变化,因为回调函数没有对您创建的实际new EventEmitter对象的引用。 (Again, unless event has a reference to the new EventEmitter object you have created.) (同样,除非event引用了您创建的new EventEmitter对象。)

In reality things might be a little different since garbage collectors in browsers are fairly complex. 实际上,情况可能有所不同,因为浏览器中的垃圾收集器相当复杂。 At what time the interpreter decides to collect the garbage is really up to it, but the bottom line is that it will only do it when all references to an object are gone. 解释器在什么时候决定收集垃圾确实由其决定,但最重要的是,只有在对一个对象的所有引用都消失时,它才会这样做。

Memory management usually isn't really a concern is JavaScript and you shouldn't need to be thinking about it most of the time. 内存管理通常并不是JavaScript真正关心的问题,您不必在大多数时间都在考虑它。 You will know it if there is a memory leak and able to detect it by using developer tools provided by the browser. 如果存在内存泄漏并且可以使用浏览器提供的开发人员工具检测到它,您将知道它。

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

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