The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Cleaning Up Unused Objects

Many other object-oriented languages require that you keep track of all of the objects you create and that you explicitly destroy them when they are no longer needed. This technique of managing memory is tedious and often error-prone. Java allows you to create as many objects as you want (limited, of course, by what your system can handle), and you never have to worry about destroying them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are naturally dropped when the variable goes out of scope. Or you can explicitly drop an object reference by setting the variable to null.

The Garbage Collector

The Java platform has a garbage collector that periodically frees the memory used by objects that are no longer needed. The Java garbage collector is a mark-sweep garbage collector. A mark-sweep garbage collector scans dynamic memory areas for objects and marks those that are referenced. After all possible paths to objects are investigated, unmarked objects (unreferenced objects) are known to be garbage and are collected. (A more complete description of Java's garbage collection algorithm might be "a compacting, mark-sweep collector with some conservative scanning.")

The garbage collector runs in a low-priority thread and runs either synchronously or asynchronously depending on the situation and the system on which Java is running. It runs synchronously when the system runs out of memory or in response to a request from a Java program.

The Java garbage collector runs asynchronously when the system is idle, but it does so only on systems, such as Windows 95/NT, that allow the Java runtime environment to note when a thread has begun and to interrupt another thread. As soon as another thread becomes active, the garbage collector is asked to get to a consistent state and terminate.

Finalization

Before an object gets garbage-collected, the garbage collector gives the object an opportunity to clean up after itself through a call to the object's finalize method. This process is known as finalization.

Most programmers don't have to worry about implementing finalize. In rare cases, however, a programmer might have to implement a finalize method to release resources, such as native peers, that aren't under the control of the garbage collector.

The finalize method is a member of the Object class, which is the top of Java's class hierarchy and the parent of everything. A class can override the finalize method to perform any finalization necessary for objects of that type. If you override finalize, then your implementation of the method should call super.finalize as the last thing it does. Overriding Methods(in the Learning the Java Language trail) talks more about how to override methods.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form