Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event handlers, closures and garbage collection in Javascript

Tags:

I'm not running into a memory leak in my application yet, but I'm worried about possible problems in the future. I would like to know if doing something like this:

SomeClass.prototype.someMethod= function() {     var that= this     this.$div2.click(function() {         that.someMethod2();     }); } 

And lets say that this.$div2 is appended to another div this.$div1. If I call

this.$div1.remove(); 

and later loses the reference of my SomeClass instance does the SomeClass instance gets garbage collected? And what about the HTML element this.$div2? this.$div2 would not be inside the DOM because it is appended to this.$div1.

I ask this because the event handler in this.$div2 might keep a reference to the HTML element this.$div2 and also keeps a reference to the instance of SomeClass through the closure because of the variable "that".

So should I care about properly removing all events and HTML elements like this? Or simply removing the "root" element (this.$div1) solves the problem?

like image 364
Hoffmann Avatar asked Dec 14 '12 13:12

Hoffmann


People also ask

What are event handlers in JavaScript?

JavaScript Event Handlers Event handlers can be used to handle and verify user input, user actions, and browser actions: Things that should be done every time a page loads. Things that should be done when the page is closed. Action that should be performed when a user clicks a button.

What is garbage collection in JavaScript?

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC). The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

Are event listeners garbage collected?

It no longer holds references to signal or controller . With those gone, the browser realises signal can no longer receive events, and that event listener will never be called, so it can be garbage collected along with anything it references.

Is there a garbage collector in JavaScript?

There's a background process in the JavaScript engine that is called garbage collector. It monitors all objects and removes those that have become unreachable.


1 Answers

this.$div2 is appended to this.$div1. If I call this.$div1.remove(); and later lose the reference of my SomeClass instance does the SomeClass instance gets garbage collected?

Yes, when all references to it are lost - also those through event handlers , - the instance can get garbage-collected.

And what about the HTML element this.$div2? this.$div2 would not be inside the DOM because it is appended to this.$div1.

It does not matter whether it is currently attached to the DOM. If some non-collectible object references $div1, it also could access its child node $div2 and that one's event handlers, so the instance referenced from the handler would not be collectible.

I ask this because the event handler in this.$div2 might keep a reference to the HTML element this.$div2 and also keeps a reference to the instance of SomeClass through the closure because of the variable "that".

That's a circular reference and should get handled well by the engines (when none of the objects inside the circle is referenced from outside it can get collected). However, (old?) Internet Explorers fail to do this when a DOM object is involved in the circle.

For that reason the .remove jQuery method (code) internally calls the (internal) cleanData method which detaches all event listeners.

So should I care about properly removing all events and HTML elements like this? Or simply removing the "root" element (this.$div1) solves the problem?

Yes, calling remove on a jQuery wrapper automatically removes all events (from all child elements) and DOM nodes.

like image 107
Bergi Avatar answered Sep 24 '22 03:09

Bergi