Link to home
Start Free TrialLog in
Avatar of techbro
techbroFlag for United States of America

asked on

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
    }
}

Open in new window

SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of techbro

ASKER

Thank you for your response.

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of techbro

ASKER

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
	}
}

Open in new window

Avatar of phoffric
phoffric

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.
     https://www.experts-exchange.com/help.jsp?hi=23

Good luck on your certification exam!
Avatar of techbro

ASKER

Thank you for your time!