Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How many objects are eligible for garbage collection?

Posted on 2011-03-10
Medium Priority
732 Views
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
Question by:techbro
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 32

Assisted Solution

phoffric earned 2000 total points
ID: 35093694
>> 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 Comment

ID: 35099190

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

LVL 32

Accepted Solution

phoffric earned 2000 total points
ID: 35107905
>> (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 Comment

ID: 35114472
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

LVL 32

Expert Comment

ID: 35114574
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 Closing Comment

ID: 35114599
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
###### Suggested Courses
Course of the Month4 days, 10 hours left to enroll