26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

426 Object-Based <strong>Program</strong>ming Chapter 8<br />

8.14 Finalizers<br />

We have seen that construc<strong>to</strong>r methods are capable of initializing data in an object of a class<br />

when the class is created. Often, construc<strong>to</strong>rs acquire various system resources such as<br />

memory (when the new opera<strong>to</strong>r is used). We need a disciplined way <strong>to</strong> give resources<br />

back <strong>to</strong> the system when they are no longer needed <strong>to</strong> avoid resource leaks. The most common<br />

resource acquired by construc<strong>to</strong>rs is memory. <strong>Java</strong> performs au<strong>to</strong>matic garbage collection<br />

of memory <strong>to</strong> help return memory back <strong>to</strong> the system. When an object is no longer<br />

used in the program (i.e., there are no references <strong>to</strong> the object), the object is marked for garbage<br />

collection. The memory for such an object can be reclaimed when the garbage collec<strong>to</strong>r<br />

executes. Therefore, memory leaks that are common in other languages like C and<br />

C++ (because memory is not au<strong>to</strong>matically reclaimed in those languages) are less likely <strong>to</strong><br />

happen in <strong>Java</strong>. <strong>How</strong>ever, other resource leaks can occur.<br />

Performance Tip 8.5<br />

Extensive use of local variables that refer <strong>to</strong> objects can degrade performance. When a local<br />

variable is the only reference <strong>to</strong> an object, the object is marked for garbage collection as the<br />

local variable goes out of scope. If this happens frequently in short time periods, large numbers<br />

of objects could be marked for garbage collection, thus placing a performance burden<br />

on the garbage collec<strong>to</strong>r. 8.5<br />

Every class in <strong>Java</strong> can have a finalizer method that returns resources <strong>to</strong> the system.<br />

The finalizer method for an object is guaranteed <strong>to</strong> be called <strong>to</strong> perform termination housekeeping<br />

on the object just before the garbage collec<strong>to</strong>r reclaims the memory for the object.<br />

A class’s finalizer method always has the name finalize, receives no parameters and<br />

returns no value (i.e., its return type is void). A class should have only one finalize<br />

method that takes no arguments. Method finalize is defined originally in class Object<br />

as a placeholder that does nothing. This guarantees that every class has a finalize<br />

method for the garbage collec<strong>to</strong>r <strong>to</strong> call.<br />

Good <strong>Program</strong>ming Practice 8.10<br />

The last statement in a finalize method should always be super.finalize(); <strong>to</strong> ensure<br />

that the superclass’s finalize method is called. 8.10<br />

Software Engineering Observation 8.20<br />

The garbage collec<strong>to</strong>r is not guaranteed <strong>to</strong> execute; therefore, an object’s finalize method<br />

is not guaranteed <strong>to</strong> get called. You should not architect classes that rely on the garbage<br />

collec<strong>to</strong>r calling an object’s finalize method <strong>to</strong> deallocate resources. 8.20<br />

Finalizers have not been provided for the classes presented so far. Actually, finalizers<br />

are rarely used in industrial <strong>Java</strong> applications. We will see a sample finalize method<br />

and discuss the garbage collec<strong>to</strong>r further in Figure 8.20.<br />

Testing and Debugging Tip 8.4<br />

Several professional <strong>Java</strong> developers who reviewed this book indicated that method finalize<br />

is not useful in industrial <strong>Java</strong> applications, because it is not guaranteed <strong>to</strong> get called.<br />

For this reason, you should search your <strong>Java</strong> programs for any use of method finalize<br />

<strong>to</strong> ensure that the program does not rely on calls <strong>to</strong> method finalize for proper deallocation<br />

of resources. In fact, you might consider removing method finalize entirely and<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!