wood burning stoves*
The moose likes Beginning Java and the fly likes garbage collector question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "garbage collector question" Watch "garbage collector question" New topic
Author

garbage collector question

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
Which of the following statements are true?

1) finalize will always run before an object is garbage collected
2) finalize may run before or after an object is garbage collected
3) finalize will run when an object becomes unreachable
4) finalize allows a programmer to free memory allocated to an object


I was trying above question from link
http://www.jchq.net/certkey/0301certkey.htm

Finalize will always be run before an object is garbage collected. It cannot run after it is collected because by then the object will cease to exit. When an object becomes unreachable it will be eligible for garbage collection but there is no guarantee when finalize will run, only that it will run before garbage collection happens. The final option is a passable description of destructors in C++ but not of the finalize method in Java.

I have not understood clearly explanation in terms of relation between garbage collection and finalize method. Any ideas, resources, links, sample code highly appreciated. thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Effective Java by Joshua Bloch; there are details here.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: garbage collector question