Java Reference
In-Depth Information
is zero, it becomes garbage. This algorithm has a lot of overhead of updating the reference count of objects. Another
type of algorithm, which is called a tracing algorithm , is based on the concept of a root set. A root set includes
Reference variables in the Java stack for each thread
Static reference variables defined in loaded classes
Reference variables registered using the Java Native Interface (JNI)
A garbage collector, which is based on the tracing algorithm, starts traversing references starting from the root set.
Objects that can be reached (or accessed) from the reference variables in the root set are known as reachable objects .
A reachable object is considered live. A reachable object from the root set may refer to other objects. These objects are
also considered reachable. Therefore, all objects that can be reached directly or indirectly from the root set reference
variables are considered live. Other objects are considered dead and are thus eligible for garbage collection.
An object may manage resources other than memory on heap. These resources may include network
connections, file handles, memory managed explicitly by native code, etc. For example, an object may open a file
when it is created. File handles that can be opened simultaneously may have an upper limit depending on your
operating system. When the object is garbage collected, you may want to close those file handles. The garbage
collector gives the dying object a chance to perform the cleanup work. It does this by executing a predefined block of
code before the memory for the dying object is reclaimed. The process of performing the cleanup work, before the
object is reclaimed by the garbage collector, is known as finalization . The block of code that is invoked by the garbage
collector to perform finalization is known as the finalizer . In Java, you can define an instance method finalize()
in a class, which serves as a finalizer for the objects of that class. The Java garbage collector invokes the finalize()
method of an object before it reclaims the memory occupied by the object.
Invoking the Garbage Collector
Programmers have little control over the timing when the garbage collector is run. The JVM performs the garbage
collection whenever it runs low in memory. The JVM tries its best to free up memory of all unused objects before it
throws a java.lang.OutOfMemoryError error. The gc() method of the java.lang.Runtime class may be used to pass
a hint to the JVM that it may run the garbage collector. The call to the gc() method is just a hint to the JVM. The JVM is
free to ignore the call. The Java Language API Documentation describes the behavior of a call to the gc() as follows:
“Runs the garbage collector. Calling this method suggests that the Java virtual machine expend
effort toward recycling unused objects in order to make the memory they currently occupy available
for quick reuse. When control returns from the method call, the virtual machine has made its best
effort to recycle all discarded objects . . .”
Suggesting that the garbage collection should run can be invoked as shown:
// Get the Runtime instance
Runtime rt = Runtime.getRuntime();
// Invoke the garbage collector
rt.gc();
You can combine the above two statements into one statement, if you do not intend to use the Runtime instance,
like so:
// Get runtime instance and invoke the garbage collector
Runtime.getRuntime().gc();
 
Search WWH ::




Custom Search