Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How many objects are eligible for garbage collection?

Posted on 2011-03-10
6
Medium Priority
?
738 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 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

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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

971 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