File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I am confused on GC 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 "I am confused on GC" Watch "I am confused on GC" New topic
Author

I am confused on GC

mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
Which of the following statements are true?
1 A reference to a primitive is eligible for garbage collection when it is set to zero.
2 A reference to a primitive is eligable for garbage collection when it is set to null.
3 An array object is eligible for garbage collection when it is no longer reachable.
4 Setting an object reference to null will guarantee the object is eligable for garbage collection.


I thought answer should be 2 and 3,4 but its just 3. ..can any one please expalin me why only 3 and not others

Thanks Javaranchers
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
4 is definitely not as the object might be referenced by other reference variable, so its not guaranteed. Also nothing is guaranteed for GC, only something might be eligible for GC.

I am not sure about # 2, but I think primitive stays in stack not heap, so its not eligible for GC. If, I am wrong, please someone correct.

Thanks,
Mohammad

[ October 17, 2008: Message edited by: Mohammad Khan ]

[ October 17, 2008: Message edited by: Mohammad Khan ]
[ October 17, 2008: Message edited by: Mohammad Khan ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14139
    
  18

mukki pandey, when you copy a question from a mock exam, book or other source, you are required to quote your sources. Please tell us where you copied this from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
sumi rankan
Ranch Hand

Joined: Apr 07, 2008
Posts: 46
Which of the following statements are true?
1 A reference to a primitive is eligible for garbage collection when it is set to zero.


false.Assuming "reference to primitive" is primitive wrapped in an object and referred by a variable,is eligible for gc only when this is the single live reference to that wrapper.

2 A reference to a primitive is eligable for garbage collection when it is set to null.

false .Again by the same assumption,the object will only be eligible if there is no other reference other than the one set to null.Setting one reference to null will not make the object eligible for gc.

3 An array object is eligible for garbage collection when it is no longer reachable.

true.Since it has been explicitly given that the object is no longer reachable.

4 Setting an object reference to null will guarantee the object is eligable for garbage collection.

false.There is no guarantee that if one reference is set to null,the object will be eligible for gc.There may be some other references.

So ,unless it has been given explicitly that there are no live references to this object or this object is no longer reachable,that particular object will not be eligible for gc.
[ October 17, 2008: Message edited by: sumi rankan ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by mukki pandey:
... A reference to a primitive...

That's some strange wording.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
Thanks sumi

Source is Examulator .Question a day
Raj Gahalot
Greenhorn

Joined: Aug 07, 2008
Posts: 7
You can not force to gc() to garbage collection, you can only put a request.
Its purely depends on JVM, which object is eligible for gc. So ans. 3 is correct.


Raj<br />Preparing for SCJP1.5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

I agree with Marc. Reference to a primitive variable makes no sense, it must be primitive variable(in which case garbage collection makes no sense) or it must be reference to a wrapper class..


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I am confused on GC