aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

RajeshParab
Greenhorn

Joined: Feb 08, 2000
Posts: 14
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

Joined: Dec 14, 1998
Posts: 20526
    ∞

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.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 21, 2000
Posts: 159
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

Joined: Nov 22, 2008
Posts: 18944
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

 
 
subject: Garbage Collection