Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Cleaning Up Unused Objects

Many other object-oriented languages require that you keep track of all the objects you create and that you destroy them when they are no longer needed. Writing code to manage memory in this way is tedious and often error-prone. Java saves you from this by allowing you to create as many objects as you want (limited of course to whatever your system can handle) but never having to destroy them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is known as 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 value of a variable whose data type is a reference type to null.

The Garbage Collector

The Java runtime environment 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 that scans Java's dynamic memory areas for objects, marking those that are referenced. After all possible paths to objects are investigated, those objects that are not marked (that is, not referenced) 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 both synchronously and asynchronously depending on the situation and the system on which Java is running.

The garbage collector runs synchronously when the system runs out of memory or in response to a request from a Java program. Your Java program can ask the garbage collector to run at any time by calling System.gc. Forcing Finalization and Garbage Collection(in the Writing Java Programs trail) has details.

Note: Asking the garbage collection to run does not guarantee that your objects will be garbage collected.

On systems that allow the Java runtime to note when a thread has begun and to interrupt another thread (such as Windows 95/NT), the Java garbage collector runs asynchronously when the system is idle. As soon as another thread becomes active, the garbage collector is asked to get to a consistent state and then 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.

During finalization an object may wish to free system resources such as files and sockets or drop references to other objects so that they in turn become eligible for garbage collection.

The finalize method is a member of the Object class. A class must override the finalize method to perform any finalization necessary for objects of that type. Writing a finalize Method later in this lesson shows you how to write a finalize method for your classes.


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces