| Developer's Daily | Java Education |
| front page | java | perl | unix | DevDirectory |
Introduction
As you know, the Java Virtual Machine (JVM) takes care of memory management for you. That's one of the beauties of Java - life as a Java developer is easier than C/C++ because you don't have to worry (as much) about memory management, and making all of the mistakes associated with memory management, such as forgetting to free dynamically allocated memory.
Generally speaking (and ignoring the differences between implementations and versions) the JVM works by performing garbage collection when it needs more memory to continue execution. Though your program may lose a reference to an object - thereby making it a candidate to be reclaimed - there's no guarantee the JVM will reclaim that memory.
Although you can't tell Java to explicitly reclaim the memory of a specific
de-referenced object, there is a way to tell the JVM to perform garbage
collection, which may result in the desired memory being reclaimed.
It's a very simple technique, and I was inspired to use it when I was developing
a Java-based editor on a computer with limited memory itself.
Invoking the garbage collector
Invoking the garbage collector requires a simple two-step process. First you create a Java Runtime object. If you haven't used them before, Runtime objects let you interface with the environment in which your application is running. Then, after creating the Runtime object, you'll invoke the gc() method ("garbage collector") of the Runtime class.
Written in Java, these two steps look like this:
That's all that's required in this technique. Let's look at how
this works in a test program.
A simple test program
Listing 1 shows a simple program that tests this technique. Although it may look a little complicated, all we do is grab a bunch of memory and then release it. At various stages in the program, we use the freeMemory() method of the Runtime class to determine the amount of free memory in the system.
| // source code developed by DevDaily Interactive, Inc.
public class GCTest { final int NELEMS = 50000; void eatMemory() { int[] intArray = new int[NELEMS]; for (int i=0; i<NELEMS; i++) {
} public static void main (String[] args) { GCTest gct = new GCTest(); // Step 1: get a Runtime object
// Step 2: determine the current
amount of free memory
// Step 3: consume some memory
// Step 4: determine amount of memory
left after consumption
// Step 5: run the garbage collector,
then check freeMemory
} }
|
| Listing 1: | The GCTest program (GCTest.java) demonstrates how you can manually invoke the garbage collector of the JVM. |
To compile this program at the command line, just enter this command:
Running the same byte code on various Unix workstations, I get different numbers, but a similar amount of memory consumption indicated by the second print statement.
As you can see, the memory initially consumed by our array in the eatMemory()
method is not immediately reclaimed by the interpreter. Only after
we call the gc() method is the memory reclaimed, even though the
eatMemory() method has already been invoked, and control has returned
to the main() method.
Download the source code
If you'd like to download the source code shown in Listing 1, just click here, and the Java source code will be displayed in your browser. Then just use the "File | Save As" option of your browser to save the source code to your system.
Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.