Saturday, October 13, 2012

Reduce finalizer usage


IBM Debugging OutOfMemory Errors and Performance Problems


Reduce finalizer usage

Use the smallest number of finalizers possible, and ensure that they run as quickly as possible
Introduction
Finalization of an object occurs when the object is no longer in use, but before it is removed from the heap. Only the Garbage Collector can determine if an object is no longer in use and is eligable for collection. When the Garbage Collector runs, it finds the unreachable objects that have a finalizer method and adds them to the finalizer queue. Because methods cannot be run during the stop-the-world phase of garbage collection, the removal of objects with finalizers is delayed until the garbage collection cycle after the finalizer method has been run. Where possible, this removal occurs in the next garbage collection cycle and collection is delayed only by a single cycle. If this removal does not occur, a backlog of objects requiring finalization can build up.
Ensuring objects are finalized before the next garbage collection cycle
A single thread is responsible for running the finalizer method of all objects with a finalizer method, and therefore this activity occurs serially. To ensure that finalization occurs before the next garbage collection cycle, follow these guidelines:
Use finalizers only where necessary
By reducing the number of finalizers, all finalization is more likely to occur between garbage collection cycles. Where possible, do cleanup synchronously without the use of finalizers.
Ensure that finalizers complete quickly
Finalizers must run as quickly as possible and avoid taking locks or blocking on external resources or connections.

Use the smallest number of finalizers possible, and ensure that they run as quickly as possible

No comments:

Post a Comment