x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 742

# How many objects are eligible for garbage collection?

I am studying for a certification exam - "Oracle Certified Professional, Java SE 6 Programmer". I approached this GC problem in my exam simulator software - Enthuware. I need help with Garbage Collection in Java.

How many objects are eligible for garbage collection right after line marked //1 and //2?

According to my calculation,
-      Zero objects are eligible for GC after line 1
-      5 objects (Train, Engine and 3 Bogie Objects) are eligible for GC after line 2.
But my exam simulator’s answer is 0 and 4. I like to verify whether it is the right answer, and why?

``````class Engine{ }
class Bogie {  Bogie next; }
class Train
{
Engine e = new Engine();
Bogie b;
public Train(Bogie b){ this.e = e; this.b = b; }
}

class TestClass
{
public static void main(String[] args)
{
Bogie b = new Bogie();
b.next = new Bogie();
Train t = new Train(b);
b = null; //1
t = null; //2
}
}
``````
0
techbro
• 3
• 3
2 Solutions

Commented:
>> Train, Engine and 3 Bogie Objects
But only 2 Bogie Objects were new'd. When new Train is collected, it also takes out the Train's member Bogie b.
0

Author Commented:

I still do not understand the concept clearly. Here is what I know:

When the object Bogie is created by "new", another pointer to Bogie is allocated - "Bogie next". (1 Bogie Object + 1 Internal Pointer to Bogie) = 2
When Object Train is created, 2 more internal pointer to Train is also allocated. (1 Train Object +  1 Engine + 1 Internal Pointer to Train) = 3

So when b and t are null, all of the above 5 Objects are eligible for GC in the end?
Please let me know if I got it right.
0

Commented:
>> (1 Train Object +  1 Engine + 1 Internal Pointer to Train)
Not sure what you mean by 1 Internal Pointer to Train

If you use the NetBeans IDE 6.9.1, then in the Variables window, "in some cases, the debugger assigns a pound sign (#) and a number as the variable's value. This number is an unique identifier of the given instance. You can use this identifier to determine if a variable points to the same instance or to a different instance."

You will be able to see that only 4 instances were created. You can also see this by adding a print statement in each constructor - I included the instances also. (In Train constructor, I put two print statements upon entering and exiting the constructor.)

Now step through the program and observe that there are 4 distinct objects created by your code.

>> When the object Bogie is created by "new", another pointer to Bogie is allocated - "Bogie next". (1 Bogie Object + 1 Internal Pointer to Bogie) = 2

Bogie b = new Bogie();
Here only one Bogie instance is created

b.next = new Bogie();
Here only one Bogie instance is created

Train t = new Train(b);
Here only one Train instance is created

As part of this Train instance creation, there is
Engine e = new Engine();
Here only one Engine instance is created which occurs before the body of the Train constructor is invoked.

In the Train constructor,
this.e = e;
The RHS is the Engine instance that was just created, and is the same as this.e; so this statement is a do-nothing statement. As you can see from the below output, there is no change to the Engine member before and after this statement.

this.b = b;
Now the Train instance is just setting its member to refer to the Bogie instance created in main (i.e., gc.Bogie@126e85f).

OUTPUT:
Creating Bogie: gc.Bogie@126e85f
Creating Bogie: gc.Bogie@129f3b5
Creating Engine: gc.Engine@11121f6
Enter Train ctor: e: gc.Engine@11121f6 b: null
Exit  Train ctor: e: gc.Engine@11121f6 b: gc.Bogie@126e85f
0

Author Commented:
I really appreciate your response. Before closing, I have one more problem related to this question.
I approached this problem in a book written by "Sierra-Bates", where I need to solve multiple choice question.
When the //do stuff is reached, how many objects are eligible for GC?

The code is given below, and I also use print statement inside the constructor to count the number of Objects created.

Obviously, 2 Objects are created, and it looks like 1 Object is eligible for Garbage Collection.
However, the answer is 2 Objects (given in the book). The explanation in the book is, "Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible"

My Question is, why the "Short story" variable is counted as an Object (which is also eligible for GC), whereas, "Bogie next" varible inside the Beta Object in the previous problem is not counted as an Object?

``````class CardBoard
{
Short story = 200;
CardBoard()
{
System.out.println("CardBoard Object: " + toString());
}

public static void main (String [] args)
{
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
c1 = null;
//do stuff
}
}
``````
0

Commented:
While this is related to GC, it is a different question. For the PAQ it would be really useful if you could come up with titles as closely related to the type of problem that you are facing. If you wish, you do not have to close this thread until you receive a satisfactory answer to the new question, especially if you think the answer there may affect the answer here. For example, you may try to squeeze in Wrapper into this new question.
http://www.experts-exchange.com/help.jsp?hi=23

Good luck on your certification exam!
0

Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.