• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Garbage Collection

 
Greenhorn
Posts: 14
  • 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 about garbage collection are true?
Select all valid answers

a) Only objects with no references will be garbage collected
b) Unused objects can be immediately garbage collected by setting them to null
c) Unused objects can be immediately garbage collected by executing 'System.gc()'
d) Setting an object to null will make it availabe for garbage collection
e) Garbage collection cannot be forced
answer is d & e
i thought a, d & e. why not a???
 
Trailboss
Posts: 23667
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally, I would have to agree with you.
I think the point they are trying to make is that you can have this situation:
myReferenceA -> myObjectA -> myObjectB -> myObjectC
If I set myReferenceA to null, then myObjectC will have a reference to it, but it too will be available for GC.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is No guarantee that Garbage collection Thread will Run,Hence we cannot surely say that if there are no references to Object It will be garbage Collected.Hence a is not TRUE. only D & E are True
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read pt(a) carefully:
"only objects with no references will be garbage collected"
suppose we have an object a set to null. now, if another object b has a reference to a, then we can say that BOTH a and b are available for garbage collection.
this means that although b has a reference to a, it can be garbage collected.

PS: this question should be reworded to
"only objects with no references can be garbage collected"
(replace "will" by "can", as there is no guarantee of garbage collection)
HTH, chetan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI chetan Well lets take ur example
"suppose we have an object a set to null. now, if another object b has a reference to a, then we can say that BOTH a and b are available for garbage collection.
this means that although b has a reference to a, it can be garbage collected."
Here the point is a and b you are talking about are references(pointers) to an Object (no names for Object) which is created some where in the Heap memory.and a and b are references referencing the location of Object.Well once a is set to Null and b is pointing to a(same as setting b to NULL)there is no way to reach an Object.Hence all its refrence counters Drops down to ZERO.Hence the Object becomes Eligible for Garbage Collection.
But the Senetence
"only objects with no references will be garbage collected"
Sounds like all the Objects for which Reference counter is Zero Will definitly GC-ed.
But It is pretty Much sure that garabage Collection thread might not run at all throughout the program or even if it Runs it is not sure that it will reclaim memory of all the eligible Objects.
Hence we cant say that "only objects with no references will be garbage collected"
Well as u said if the wordings are like
"only objects with no references can be garbage collected" OR
"only objects with no references May be garbage collected"
Then a would have also been an answer.
So answer is ONLY D & E not a.
Hope This will Helps
Sharana

 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic