1

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 ?

6
  • 3
    objectA = null; only sets the value of the variable objectA to null. It does not change the value of the property objectB.pointer. objectB.pointer still refers to an object. Variables / properties are just containers for values. An assignments "copies" the value and now two containers contain the "same" value (each has their own copy). Replacing the value in one of the containers doesn't magically replace the value in another container. Commented Dec 27, 2016 at 20:29
  • Setting objectA to null does not change objectB.pointer Commented Dec 27, 2016 at 20:29
  • @FelixKling: How could objectB.pointer refer to the object if objectB is set to null ? Commented Dec 27, 2016 at 20:36
  • 2
    Again, variables and properties are containers. When you do objectB.pointer = objectA; then you are copying the content from one container to another. In other words objectB.pointer does not refer to the container objectA but to the value that it has/had. Later when you do objectA = null; you are changing the content of container objectA. That has no impact on objectB.pointer. Here is a simpler example: var foo = 42; var bar = foo; foo = 0;. bar has still the value 42 even though I set foo to 0. Changing the value of foo has no impact on bar. Commented Dec 27, 2016 at 20:40
  • @FelixKling I understand how copying between primitive and reference values works. What I am talking about is this: In my example there are two objects. At beginning they have one reference - first object has reference in objectA and second object has reference in objectB. Then second reference is created for these objects. First object has second reference in objectB.pointer and second object has second reference in objectA.pointer. So how we could decrease references for both objects to zero ? I assumed that this is achieved by setting both objectA and objectB to null. Commented Dec 27, 2016 at 21:01

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh beautiful ascii art ! Thanks, this is explained very well. I clearly understand the problem now. I was blind :) Thanks again
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.