aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on Garbage collection" Watch "doubt on Garbage collection" New topic
Author

doubt on Garbage collection

ansuman mohapatra
Greenhorn

Joined: Mar 04, 2008
Posts: 27
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }


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.

answer given as C. Short wrapper along with c1 object. But i doubt why c2 and c3 arent garbage collected ? Is it because we are passing object reference to the method so it cant be changed further ?


Cleared SCJP.....
Mihai Fonoage
Ranch Hand

Joined: Feb 25, 2006
Posts: 39
This is because when calling the go() method, you are passing a copy of the reference variable c2, not the c2 reference variable itself, nor the object that c2 points to. This is why the object that c2 refers to is not yet ready to be garbage collected. Furthermore, in the go() method you are not creating any new object, so c3 never actually pointed to an object. Basically, c1 (together with the Short object) would be GC.

Mihai Fonoage
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

ansuman, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Have you tried doing a search on this forum? This exact question comes up every few days or so.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
why the Short object is elligible for gc ?
Mihai Fonoage
Ranch Hand

Joined: Feb 25, 2006
Posts: 39
Think about it, how would you use the Short instance variable of c1, if c1 == null? (I believe this is called isolating a reference)

Mihai Fonoage
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Originally posted by Ramesh Sahu:
why the Short object is elligible for gc ?


It's not. The Short object is still in the cache (for autoboxing). There is an errata for this question, due to this reason.

Henry
ansuman mohapatra
Greenhorn

Joined: Mar 04, 2008
Posts: 27
Originally posted by Henry Wong:


It's not. The Short object is still in the cache (for autoboxing). There is an errata for this question, due to this reason.

Henry


The answer is given as

"Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible."
Mihai Fonoage
Ranch Hand

Joined: Feb 25, 2006
Posts: 39
You are right Henry (but you probably already new that ). I forgot about the -128 to 127 range for Short and Integer.

Mihai Fonoage
Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
So, this means that object(s) that reside in the pool, e.g String pool or wrapper pool, will be eligible for GC ?


Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
Mihai Fonoage
Ranch Hand

Joined: Feb 25, 2006
Posts: 39
Details answering your question can be found here.

Mihai Fonoage
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on Garbage collection