javascript circular reference - breaking the cycle

112 views Asked by At

For example, I have this code:

var objectA = {};
var objectB = {};

objectA.pointer = objectB;
objectB.pointer = objectA;

objectA = null;
objectB = null;

I assume that after last two statements, there is no reference to objects originally assigned to objectA and objectB variables. But according to the technical literature, the reference for those objects still exists. Why ? Does objectA.pointer reference or objectB.pointer reference still exists even the objectA and objectB are set to null ?

2

There are 2 answers

1
Felix Kling On BEST ANSWER

Does objectA.pointer reference or objectB.pointer reference still exists even the objectA and objectB are set to null ?

Yes.

Maybe some ASCII art helps. After performing

var objectA = {};
var objectB = {};

the environment contains two variables (objectA and objectA) that hold references to two objects (denoted as ref:XXX):

                          +--------------+
+-------+---------+  +--->|  Object#123  |
|objectA|ref:123 *+--+    +--------------+
+-------+---------+                       
|objectB|ref:456 *+--+    +--------------+
+-------+---------+  +--->|  Object#456  |
                          +--------------+

After adding properties to the objets,

objectA.pointer = objectB;
objectB.pointer = objectA;

both objects have a pointer property each contain a reference to the other object:

                         +-----------------+      
                         |   Object#123    |      
                    +--->+-------+---------+<----+
+-------+---------+ |    |pointer|ref:456 *+---+ |
|objectA|ref:123 *+-+    +-------+---------+   | |
+-------+---------+                            | |
|objectB|ref:456 *+-+    +-----------------+   | |
+-------+---------+ |    |   Object#456    |   | |
                    +--->+-------+---------+<--+ |
                         |pointer|ref:123 *+-----+
                         +-------+---------+      

As you can se see, there is no relation between a pointer property and the objectA and objectB variables. objectA.pointer doesn't refer to the variable objectB, it got a copy of its value (ref:456), a reference to the object.

After setting both variables to null,

objectA = null;
objectB = null;

the environment looks like this:

                         +-----------------+      
                         |   Object#123    |      
                         +-------+---------+<----+
+-------+---------+      |pointer|ref:456 *+---+ |
|objectA|  null   |      +-------+---------+   | |
+-------+---------+                            | |
|objectB|  null   |      +-----------------+   | |
+-------+---------+      |   Object#456    |   | |
                         +-------+---------+<--+ |
                         |pointer|ref:123 *+-----+
                         +-------+---------+      

The pointer properties still hold the references to the other object. Replacing the values of objectA and objectB didn't change that.

0
Mark Segal On

You don't have to worry about this, JS is a managed language and has an built in Garbage Collector, that will take care of this case. Read more here: https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

What you're describing is solved in various techniques. You can read in this question How does Java Garbage Collection work with Circular References? about its implementation in Java.