aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477




When // doStuff is reached, how many objects are eligible for GC?

A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

I got the answer b.Please correct me i am wrong
Rick Roberts
Ranch Hand

Joined: Apr 07, 2005
Posts: 59



Answer b (2 objects). seems to be the right answer.

Line 10 nulls c2 created on line 9 ( that's 1 object )
Line 11 nulls c1 created on line 8 ( that's 2 objects )
Line 10 assigns null to c3 which was never instantiated,
so.. ( that's still 2 objects, right? )


SCJP
"Stay on the path."
Rick Roberts
Ranch Hand

Joined: Apr 07, 2005
Posts: 59

OOPS!

b was not 2 objects.
c was 2 objects... I think 2 objects are available for GC... so answer c.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
this thread.
Riyaz Saiyed
Greenhorn

Joined: Aug 13, 2006
Posts: 22


Here two objects are eligible for garbage collection.
But which are those two?
At line no 10, no abject is created and null is assign to c3.
At line no 11, one object is eligible for gargbage collection (The one which was created at line 8).
Since CardBoard has one instance variable Short story, which is not reachable, this object also is eligible for garbage collection.
Thus CardBoard and one Short object are eligible for garbage collection.
Santhosh Raje
Greenhorn

Joined: Sep 26, 2006
Posts: 9
Hey,
Here After line 10 ,the CardBoard objects at line 9 becomes eligible for Garbage Collection since c2 is made to point to null while calling in method go().
At line 11 ,the CardBoard object present in line 8 becomes eligible.
Hence two cardboard objects becomes eligible for Garbage Collection.

I dont understand why Short object story becomes elligble for Garbage collection.
Rajesh Kadle
Greenhorn

Joined: Sep 06, 2004
Posts: 26
I think only object referenced by c1 will be eligible for garbage collection.

Since parameters are passed by value, c2 in main will not be made null, so this object is not available for garbage collection.

No object was created and referenced by c3, it was just initialized to null.

So only 1 object should be eligible for garbage collection.


-Raj
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Rajesh Kadle:
I think only object referenced by c1 will be eligible for garbage collection.

Since parameters are passed by value, c2 in main will not be made null, so this object is not available for garbage collection.

No object was created and referenced by c3, it was just initialized to null.

So only 1 object should be eligible for garbage collection.


Correct.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You can always modify the code to get an idea what is happening. But realize that System.gc may not always do what you want it to.
Riyaz Saiyed
Greenhorn

Joined: Aug 13, 2006
Posts: 22
I'm not satisfied.
This is the same example, sited in the K & B, SCJP 5 book (Page 266).
It says..
Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection
 
Similar Threads
Objects - GC
About Garbage Collection
Problem in question from K&B
GC help
Garbage collection