Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What garbage collection algorithms do all 5 major browsers use?

I am currently rethinking the object dispose handling of the qooxdoo JavaScript framework.
Have a look at the following diagram (A is currently in scope):

diagram http://yuml.me/51747906.jpg

Let's say we want to delete B. Generally, we cut all reference between all objects. This means we cut connection 1 to 5 in the example. Is this really necessary?
As far as I have read here, browsers use the mark-and-sweep algorithm. In that case, we just need to cut reference 1 (connection to the scope) and 5 (connection to the DOM) which could be much faster.
But can I be sure that all browsers use the mark-and-sweep algorithm or something similar?

like image 924
Martin Wittemann Avatar asked Apr 19 '10 12:04

Martin Wittemann


People also ask

Which algorithm is used for garbage collection?

The mark-and-sweep algorithm is called a tracing garbage collector because it traces out the entire collection of objects that are directly or indirectly accessible by the program.

Does JavaScript use garbage collection?

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.

Does Java GC stop the world?

Stop the World Event - All minor garbage collections are "Stop the World" events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events.


2 Answers

For any decent garbage collector (not only mark-and-sweep), cutting connection 1 would be enough to release B (and C and D and the window). Allocation based on reference counting would fail to release B and D due to their cyclic references (B references D and D references B) but reference counting is not really garbage collection.

I think it is safe to assume that all browsers use a decent garbage collector (well, with browsers, nothing is ever really safe, but a JavaScript implementation not using a proper garbage collector is improbable nonetheless).

like image 64
Thomas Pornin Avatar answered Nov 15 '22 15:11

Thomas Pornin


The thing is that in an ideal world, you basically just need to disconnect DOM nodes and native event listeners. Problem is though, that the original system in qooxdoo was designed around buggy browsers like IE6. We saw massively reduced memory usage when we delete as much as possible on our own. In todays world I would however redesign it in a way that it's OK in IE6, but not optimized for its problems.

There is also a difference of a complete shutdown of the whole application (dispose all) and just to dispose a single fraction of an application. In the last scenario you need to act quite carefully to not dispose stuff which is still needed.

like image 45
Sebastian Werner Avatar answered Nov 15 '22 14:11

Sebastian Werner