Objects and Classes in Java |
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. The next section discusses this in more detail.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'sfinalize
method. This process is known as finalization.During finalization, an object may wish to free system resources such as files and sockets or to drop references to other objects so that they in turn become eligible for garbage collection. The
finalize
method for theRectangle
class releases thePoint
object by setting its reference toorigin
tonull
:Theprotected void finalize() throws Throwable { origin = null; super.finalize(); }finalize
method is a member of theObject
class. TheObject
class is the top of Java's class hierarchy and the parent of everything. A class should override thefinalize
method to perform any finalization necessary for objects of that type. Note thatRectangle
'sfinalize
method callssuper.finalize
to give the parent class a chance for final cleanup as well. Generally speaking, afinalize
method should callsuper.finalize
as the last thing it does. You can read more in Writing afinalize
Method.
Objects and Classes in Java |