aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a doubt  on 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 "a doubt  on garbage collection." Watch "a doubt  on garbage collection." New topic
Author

a doubt on garbage collection.

Ajit Amitav Das
Ranch Hand

Joined: Dec 14, 2005
Posts: 49
Hi Ranchers
I got a doubt in the question given below.
Question :
If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection

1.The compiler will throw an error.
2.The garbage collector "collects" the object anyway.
3.The garbage collector does not collect the object in the present sweep. But when the object becomes eligible for garbage collection again, its finalize method will not be called by the garbage collector (i.e., if the garbage collector gets a chance to run.). It will simply be garbage collected.
4.The object can never be garbage collected and hence leads to memory-leak. Each time the garbage collector calls finalize before "collecting" the object the object "resurrects" itself.

To me i thinks it's option-4 but actual answer is given option-3 , is grabage collector behave like that way in those situations.


Warm Regards<br />Ajit Amitav Das<br />SCJP 1.5
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
Hi Ajit,

If you go through the Java Docs for finalize() you will find explanation for the answer being (c).


After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.
Ajit Amitav Das
Ranch Hand

Joined: Dec 14, 2005
Posts: 49
thanks pradeep, thanks a lot.
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Hi ajit

well i didnt got the question .

If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection

what does 're-refrences an object' means .

Thanking you in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Originally posted by Anvi Dixit:
Hi ajit

well i didnt got the question .

If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection

what does 're-refrences an object' means .

Thanking you in advance


For an object to make itself no long eligible for garbage collection, it needs to have a reference to it from something that is still reachable.

So it needs to have itself (ie. "this") assigned from some reference in some other object that is still reachable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
What is resurrection (in garbage collection)?
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

See this,

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The exact same information as the above listing is to be found in class Object's finalize method API documentation. So there is no need to go to the extent of getting the source code in this case - especially because it is more readable in the API.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a doubt on garbage collection.
 
Similar Threads
finalize() method doubt........
finalize() question
garbage collector question
GC doubt
finalize()