Study guide: Trash talk, Part 1
Brush up on Java terms, learn tips and cautions, review homework assignments, and read Jeff's answers to student questions
By Jeff Friesen, JavaWorld.com, 12/07/01
Glossary of terms
- finalization
- A fallback mechanism for cleaning up finite resources. Finalization occurs when the garbage collector calls a resurrectable
object's overridden
finalize() method.
- garbage collection
- The process by which some executable, such as the JVM, automatically frees an object's memory when a single reference to that
memory no longer exists.
- garbage collector
- That part of the JVM that performs garbage collection.
- handles
- Indexes into a table of object references.
- mark-and-sweep algorithm
- A tracing-based garbage collector algorithm that first marks in some way each object during a trace through all objects on
the object heap and then sweeps all unmarked objects into oblivion.
- object heap
- That portion of the JVM's memory area from which the JVM allocates memory for new objects.
- reachable
- A state in which an object can be reached via some path that starts from a root-set variable.
- resurrection
- The act of preventing an object's collection by making it reachable from inside its
finalize() method.
- root set of references
- A group of reference variables always accessible to an executing Java program. Those variables include local variables, parameters,
and class fields.
- stop-and-copy algorithm
- A copying-based garbage collector algorithm that divides the object heap into an object area and a free space area. The stop-and-copy
garbage collector stops program execution while it copies objects from the object area to the free space area, defragmenting
the heap in the process.
- Train algorithm
- A modified generational collector algorithm that manages the mature object space to shorten the delay that the garbage collector
imposes on a program.
- unreachable
- A state in which an object cannot be reached by any path starting from any root-set variable.
Tips and cautions
These tips and cautions will help you write better programs and save you from agonizing over error messages produced by the
compiler.
Tips
- Get into the habit of ending your
finalize() methods with super.finalize(); method calls. That way, if you insert a superclass above your class and if that superclass has its own finalize() method, you can be sure the garbage collector calls superclass's finalize() method.
Cautions
- You should call
System.runFinalization (); immediately after a call to System.gc ();. If you place System.runFinalization (); before System.gc (); or omit the System.gc (); call, not all finalize() methods will run. (I do not know the reason for this.)
- If you resurrect an object and then make that object unreachable, the next time the garbage collector runs, it will collect
that object without calling the object's
finalize() method.
Miscellaneous notes and thoughts
You might be wondering why I did not include instance (that is, object) fields in the root set of references. The reason is
that, unlike class fields (which are always accessible through a program's lifetime) and local variables/parameters (which
are always accessible during a method call), instance fields become inaccessible to the garbage collector when an object becomes
unreachable.