Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular references in Javascript / Garbage collector

Can somebody explain in detail how Javascript engines deal with circular references ? Is there a big difference between browsers or even node.js ?

What I'm talking about is an explicit back-/next reference within objects. For instance:

var objA = {     prop: "foo",     next: null };  var objB = {     prop: "foo",     prev: null };  objA.next = objB; objB.prev = objA; 

There we go. If we do a console.log( objA ) we can see that we created an infinite chain. The big question is, is this bad ? Does it create memory leaks when not explicitly cleaned?

So do we have to

objA.next = null; objB.prev = null; 

or will the garbage collectors take care of us on constellations like this?

like image 655
jAndy Avatar asked Sep 08 '11 11:09

jAndy


People also ask

Does garbage collector handle cyclic references?

yes Java Garbage collector handles circular-reference!

Can garbage collector release isolate circular references?

The . NET garbage collector can absolutely handle circular references.

Is garbage collection based on reference in JavaScript?

References. The main concept that garbage collection algorithms rely on is the concept of reference. Within the context of memory management, an object is said to reference another object if the former has access to the latter (either implicitly or explicitly).

How does garbage collector resolves circular reference issue?

To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references. The garbage collector begins with local and static and it marks each object that can be reached through their children. Through this, you can handle the issues with circular references.


1 Answers

Any half-decent garbage collector will handle cycles.

Cycles are only a problem if you do naive reference counting.

Most garbage collectors don't do ref-counting (both because it can't handle cycles, and because it's inefficient). Instead, they simply follow every reference they can find, starting from "roots" (typically globals and stack-based variables), and mark everything they can find as "reachable".

Then they simply reclaim all other memory.

Cycles are no problem because they just mean that the same node will be reached multiple times. After the first time, the node will be marked as "reachable" already, and so the GC will know that it's been there already, and skip the node.

Even more primitive GC's based on reference-counting typically implement algorithms to detect and break cycles.

In short, it's not something you have to worry about. I seem to recall that IE6's Javascript GC actually failed to handle cycles (I could be wrong, it's been a while since I read it, and it's been much, much longer since I touched IE6), but in any modern implementation, it is no problem.

The entire point in a garbage collector is to abstract away memory management. If you have to do this work yourself, your GC is broken.

See MDN for more information on modern garbage collection and the mark-and-sweep algorithms that are used.

like image 133
jalf Avatar answered Sep 20 '22 14:09

jalf