• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
RajeshParab
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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???
 
paul wheaton
Trailboss
Pie
Posts: 21183
Firefox Browser IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • 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
 
chetan nain
Ranch Hand
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • send pies
  • 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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic