THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

understand garbage collection. Each virtual machine has its own collection strategy, and some let you choose
among several. Use this mark-and-sweep model as a mental model onlydo not assume that this is how any
particular virtual machine actually works.


17.3. Finalization


You won't normally notice when an orphaned object's space is reclaimed"it just works." But a class can
implement a finalize method that is executed before an object's space is reclaimedsee "Strengths of
Reference and Reachability" on page 455. Such a finalize method gives you a chance to use the state
contained in the object to reclaim other non-memory resources. The finalize method is declared in the
Object class:


protected voidfinalize()tHRows Throwable

Is invoked by the garbage collector after it determines that this object is no
longer reachable and its space is to be reclaimed. This method might clean up
any non-memory resources used by this object. It is invoked at most once per
object, even if execution of this method causes the object to become
reachable again and later it becomes unreachable again. There is no
guarantee, however, that finalize will be called in any specific time
period; it may never be called at all. This method is declared to throw any
exception but if an exception occurs it is ignored by the garbage collector.
The virtual machine makes no guarantees about which thread will execute the
finalize method of any given object, but it does guarantee that the thread
will not hold any user-visible synchronization locks.

You should only rarely need to write a finalize method, and when you do, you should write it with great
care. If your object has become garbage it is quite possible that other objects to which it refers are also
garbage. As garbage, they may have been finalized before your finalize method is invoked and may
therefore be in an unexpected state.


Garbage collection collects only memory. When you are dealing with non-memory resources that are not
reclaimed by garbage collection, finalizers look like a neat solution. For example, open files are usually a
limited resource, so closing them when you can is good behavior. But this usually cannot wait until the
finalize phase of garbage collection. The code that asks you to perform an operation that opens a file
should tell you when it's donethere is no guarantee that your object holding the open file will be collected
before all the open file resources are used up.


Still, your objects that allocate external resources could provide a finalize method that cleans them up so
that the class doesn't itself create a resource leak. For example, a class that opens a file to do its work should
have some form of close method to close the file, enabling programmers using that class to explicitly
manage the number-of-open-files resource. The finalize method can then invoke close. Just don't rely
on this to prevent users of the class from having problems. They might get lucky and have the finalizer
executed before they run out of open files, but that is riskyfinalization is a safety-net to be used as a last resort,
after the programmer has failed to release the resource manually. If you were to write such a method, it might
look like this:


public class ProcessFile {
private FileReader file;


public ProcessFile(String path) throws
FileNotFoundException
{
file = new FileReader(path);

Free download pdf