and you reduce the likelihood of the garbage collector running during the task. Here is a method that
aggressively frees everything it can at the moment:
public static void fullGC() {
Runtime rt = Runtime.getRuntime();
long isFree = rt.freeMemory();
long wasFree;
do {
wasFree = isFree;
rt.runFinalization();
rt.gc();
isFree = rt.freeMemory();
} while (isFree > wasFree);
}
This method loops while the amount of freeMemory is being increased by successive calls to
runFinalization and gc. When the amount of free memory doesn't increase, further calls will likely do
nothing.
You will not usually need to invoke runFinalization, because finalize methods are called
asynchronously by the garbage collector. Under some circumstances, such as running out of a resource that a
finalize method reclaims, it is useful to force as much finalization as possible. But remember, there is no
guarantee that any object actually awaiting finalization is using some of that resource, so
runFinalization may be of no help.
The fullGC method is too aggressive for most purposes. In the unusual circumstance that you need to force
garbage collection, a single invocation of the System.gc method will gather most if not all of the available
garbage. Repeated invocations are progressively less productiveon many systems they will be completely
unproductive.
Exercise 17.1: Write a program to examine the amount of memory available on start up and after allocation of
a number of objects. Try invoking the garbage collector explicitly to see how the amount of free memory
changesmake sure you don't hold references to the newly allocated objects of course.
17.5. Reachability States and Reference Objects
An object can be garbage collected only when there are no references to it, but sometimes you would like an
object to be garbage collected even though you may have a specific reference to it. For example, suppose you
are writing a web browser. There will be images that have been seen by the user but that are not currently
visible. If memory becomes tight, you can theoretically free up some memory by writing those images to disk,
or even by forgetting about them since you can presumably refetch them later if needed. But since the objects
representing the images are referenced from running code (and hence reachable) they will not be released.
You would like to be able to have a reference to an object that doesn't force the object to remain reachable if
that is the only reference to the object. Such special references are provided by reference objects.
A reference object is an object whose sole purpose is to maintain a reference to another object, called the
referent. Instead of maintaining direct references to objects, via fields or local variables, you maintain a direct
reference to a reference object that wraps the actual object you are interested in. The garbage collector can
determine that the only references to an object are through reference objects and so can decide whether to
reclaim that objectif the object is reclaimed then, the reference object is usually cleared so that it no longer
refers to that object. The strength of a reference object determines how the garbage collector will
behavenormal references are the strongest references.