Solved

How many objects are eligible for garbage collection?

Posted on 2011-03-10
6
691 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:techbro
  • 3
  • 3
6 Comments
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 500 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

by:techbro
ID: 35099190
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.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:techbro
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
	}
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
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

by:techbro
ID: 35114599
Thank you for your time!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now