Garbage collecting & optimization in Java

The Garbage Tracker

A few existential questions for the Java programmer

How does Java's hidden garbage collector work ? When should one call the gc() method of a Runtime object ? What means object recycling ? Does object recycling really speed up the execution of the code ? What is the purpose of a thread's sleep() method ? What means ecology in Java ?

Java programmers will find here a few answers to these questions. First of all, checking its source code is a perequisite to fully understand this applet :

So, what is this applet doing ? Basically, it uses a simple animation pattern as a pretext to analyze the behavior of your JVM's garbage collector.

The animation pattern

The animation consists of a thread, in which a matrix of rectangles is regularly filled with new colors. The matrix is displayed on the screen, using double buffering. A timer (called sleepTime) is set so the thread is allowed to sleep between two calls to the repaint() method.

In this applet, a cycle represents 215 successive calls to the repaint() method. The cycle time is estimated by substracting the sleep time from the total time of a white-to-black (or black-to-white) animation cycle.

Java's garbage collector

The free memory, currently available in your browser's JVM (Java Virtual Machine), is calculated as a % of the total memory allocated to the JVM, as shown in the gauge. The amount of free memory drops regularly until the JVM calls its garbage collector to clean the memory : the garbage collector destroys any java object that has no more reference in the running code. Garbage collecting frees some of the JVM memory, and you'll see the gauge going up suddenly.

The garbage collector is called automatically by the JVM when the amount of free memory drops close to 0% : while programing in Java, you normally don't need to worry about this issue, unless your program uses a lot of memory.

So, when could you need to call the gc() method in your program ? In recent JVM's : never ! Except for one purpose : if you want to test (like in this applet) the performances of your garbage collector. If the garbage collector is good, you will see no difference whether you switch the Automatic GC mode on or off.

Are calls to the gc() method harmless ?

No. If you switch the Automatic GC on and off in the applet, you will see that forcing gc() calls to the garbage collector can be time-consuming, but this highly depends on your JVM. Check the source code of this applet : by switching Automatic GC to off, the only thing you are doing is to add 215 calls per cycle to the gc() method. This is where the eventual additional execution time is coming from.

Is the gc() method reliable ?

No : calling the gc() method doesn't ensure garbage collecting ! In our applet, the gc() method is called 215 times/cycle when the Automatic GC mode is set to off. Now, compare this with the actual number of memory (gauge) jumps that occur during an applet cycle : this also depends on your JVM type.

The gc() method is only a request for garbage collecting. But we never know if the garbage collector will obey that request or not.

What means object recycling ?

In our animation, 400 rectangles are filled with Color objects at every execution of the update() method. One way to achieve this is to create new Color objects at every execution of the update() method. Exiting the update() method, no reference is left refering to these Color objects, and the garbage collector will be able to destroy them. This is what the applet does when its Optimized mode is off.

When the Optimized mode is on, instead of creating new objects at every execution of the update() method, we use a global array of Color objects. These objects are created only once, and are reused at every execution of the update() method. This process is time-saving, as we don't rebuild new objects at every pass, and as we don't leave wasted food for the garbage collector.

Recycling of existing objects can also be achieved by changing object properties : changing the properties of an object is usually much less CPU-consuming than creating a new object and throwing the old one into the garbage collector.

Recycling Java objects will not only enhance the speed of your application. It also means less CPU work, a drop in energy consumption, and saving our earth's natural resources ! However, enhancements in the most recent JVM's have made object recycling a much less important issue, e.g. if your java program is only designed to run on these JVM's.

Using the sleep() method

More than simply adding a pause in a thread, the sleep() method is a gentleman's way for the program to recognize that 'I am not alone on this computer'. The sleep() method allows competing threads and programs to run together smoothly.

Is there any advantage to shorten the sleepTime parameter passed through a Thread.sleep(sleepTime) ? If you look at this applet, you will of course speed up the animation if you choose a sleepTime of 30 milliseconds instead of 100 ms. But reducing sleepTime close to zero will not bring you many benefits : as competing threads (like garbage collecting) must still go on, and often have a higher priority, your animation will soon look jerky, with little benefits in terms of speed, an you will quickly hear your computer fan running (depending on your hardware, you can test this with this applet as well !).

Fair values for the sleepTime parameter are between 10 to 50 ms in an animation. If the only purpose of your thread is e.g. to periodically refresh an image on the screen, 250 to 400 ms are usually enough.

About this applet

This applet is freeware : this means you can use it on your computer for no fee and without restriction (it also runs within appletviewers). If you want to display this applet on your web site or to distribute it by any mean, please e-mail us first for a clearance. Have a look at the information in the source code if you want to use or modify portions of this code.

The Garbage Tracker is distributed under the GNU General Public License, and is licensed to you free of charge. The software is provided "as is" : there is no support, and no warranty of any kind.

Other applets on this site :

Sound & multimedia :

Java repository & source code :

Copyright © Neural Semantics sprl