Wednesday, September 20, 2006

Java Garbage Collector

The Java garbage collector is a daemon thread – a thread that runs for the benefit of other threads. It is a mark-sweep facility that scans Java's dynamic memory areas for objects, marking those that are referenced. When Java determines that there are no longer any references to an object, it marks the object for eventual garbage collection. The "mark and sweep" garbage collection algorithm is not suitable for all applications. The new version of Java has been expanded to contain several new garbage collection algorithms. These include the "copying collector", the "parallel copying collector" and the "parallel scavenge collector". All these new algorithms stop all application threads until the garbage collection is complete.

Java's automatic garbage collector eliminates many of the memory leaks that can occur in C and C++. It runs as a low-priority thread, waiting for higher-priority threads to relinquish the processor. You can use a finalize method in a class to help return resources to the system. The finalize method runs automatically when the system runs out of memory or when runtime ends, but you can also invoke it at other times using the System.runFinalization method.

No comments: