426 Object-Based Programming Chapter 8 8.14 Finalizers We
Tuesday, May 8th, 2007426 Object-Based Programming Chapter 8 8.14 Finalizers We have seen that constructor methods are capable of initializing data in an object of a class when the class is created. Often, constructors acquire various system resources such as memory (when the new operator is used). We need a disciplined way to give resources back to the system when they are no longer needed to avoid resource leaks. The most common resource acquired by constructors is memory. Java performs automatic garbage collection of memory to help return memory back to the system. When an object is no longer used in the program (i.e., there are no references to the object), the object is marked for garbage collection. The memory for such an object can be reclaimed when the garbage collector executes. Therefore, memory leaks that are common in other languages like C and C++ (because memory is not automatically reclaimed in those languages) are less likely to happen in Java. However, other resource leaks can occur. Performance Tip 8.5 Extensive use of local variables that refer to objects can degrade performance. When a local variable is the only reference to an object, the object is marked for garbage collection as the local variable goes out of scope. If this happens frequently in short time periods, large numbers of objects could be marked for garbage collection, thus placing a performance burden on the garbage collector. Every class in Java can have a finalizer method that returns resources to the system. The finalizer method for an object is guaranteed to be called to perform termination housekeeping on the object just before the garbage collector reclaims the memory for the object. A class s finalizer method always has the name finalize, receives no parameters and returns no value (i.e., its return type is void). A class should have only one finalize method that takes no arguments. Method finalize is defined originally in class Object as a placeholder that does nothing. This guarantees that every class has a finalize method for the garbage collector to call. Good Programming Practice 8.10 The last statement in a finalize method should always be super.finalize(); to ensure that the superclass s finalize method is called. Software Engineering Observation 8.20 The garbage collector is not guaranteed to execute; therefore, an object s finalize method is not guaranteed to get called. You should not architect classes that rely on the garbage collector calling an object s finalize method to deallocate resources. Finalizers have not been provided for the classes presented so far. Actually, finalizers are rarely used in industrial Java applications. We will see a sample finalize method and discuss the garbage collector further in Figure 8.20. Testing and Debugging Tip 8.4 Several professional Java developers who reviewed this book indicated that method finalize is not useful in industrial Java applications, because it is not guaranteed to get called. For this reason, you should search your Java programs for any use of method finalize to ensure that the program does not rely on calls to method finalize for proper deallocation of resources. In fact, you might consider removing method finalize entirely and Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services