|
|
The Garbage Tracker A few existential questions for the Java programmer 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 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 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 ? Is the gc() method reliable ? 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 ? 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 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 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 :
Java repository & source code :
|