• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

I am confused on GC

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sumi

Source is Examulator .Question a day
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Not so fast naughty spawn! I want you to know about
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic