Win a copy of The Career Toolkit this week in the Jobs Discussion forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

I am confused on GC

 
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
 
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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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
 
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.
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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..
 
I'm doing laundry! Look how clean this tiny ad is:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic