• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am confused on GC

 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sumi rankan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mukki pandey:
... A reference to a primitive...

That's some strange wording.
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sumi

Source is Examulator .Question a day
 
Raj Gahalot
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic