What You Can Do To Deal With Java’s Memory Retention Problems
When you work with the Java programming language, a feature that you will want to be familiar with is called finalization.
Finalization will allow you to conduct a cleanup on objects that the garbage collector is not capable of reaching. Finalization will generally be used to recapture resources which are connected to an object. Below is an example of a basic finalization:
public class Image1 {
// pointer to the native image data
private int nativeImg;
private Point pos;
private Dimension dim;
// it disposes of the native image;
// successive calls to it will be ignored
private native void disposeNative();
public void dispose() { disposeNative(); }
protected void finalize() { dispose(); }
static private Image1 randomImg;
}
Once the Imagel becomes impossible to reach, the JVM will request a finalize technique to make sure the resource that holds the image information will be recaptured. The finalize() technique is a method which holds arbitrary information. It can access any part of the object, and in the above example it has accessed dim and pos. In addition to this, it can also allow the object to become reachable. While this technique is not recommended by some programmers, the Java programming language allows it. I will now list the steps to show the lifespan of an object which is finalized. A finalized object is one that has a class which is non-trivial.
When the object is first registered, the Java Virtual Machine will record that this object is finalized. This may slow down the fast register path that the newer JVMs contain. After this occurs, the garbage collector will decide if the object is able to be reached. It will notice that the object has been finalized, and it will then add the object to the finalization queue. It will also make sure that all the objects which can be reached are retained, and this will allow the finalizer to gain access to them. Once this has occured, the Java Virtual Machine will use what is called a finalizer thread. The finalizer thread will dequeue the object, record the finalizer of the object, and bring forth the finalize method. Once these three things have been done, the object will be finalized.
Once the object has been finalized, it will be studied by the garbage collector. When the garbage collector sees that the object cannot be reached, it will recapture the space for the object in addition to everything which can reached within this space. The garbage collector will need two cycles to recapture an object, and it will need to recapture other objects which are reachable from the object during this procedure. If the programmer is not cautious, they could create a temporary data retention problem which will be unstable. There is no guarantee that the Java Virtual Machine will request finalizers of all the objects which have been finalized. It may cancel before the garbage collector finds that some of the objects can’t be reached.
Even if you use finalization correctly, you may find that the recapture process is extended. It is possible to correct this problem by arranging the code so that it uses a "contains" instead of "extends" pattern. An example of this code can be seen here:
public class RGBImage2 {
private Image1 img;
private byte rgbData[];
public void dispose() {
img.dispose();
}
}
This example contains an appearance of Image1 instead of just extending it. If an appearance of RGBImage2 can’t be reached, the garbage collector will quickly recapture it, and it will also capture the rgbData array. After this, it will queue up the appearance of Image1 to be finalized. It is not always possible to arrange your code in the method used above. You may be required to do a bit more work to make sure the instances do not take up more than they are supposed to when they are finalized. In the example below, I will show you how this can be done:
public class RGBImage3 extends Image1 {
private byte rgbData[];
public void dispose() {
super.dispose();
rgbData = null;
}
}
As you can see, RGBImage3 is the same as RGBImage1, but it uses the dispose( ) technique, which cancels the rgbData field. The techniques discussed in this article can effectively allow you to deal with the memory retention problems that are inherent in Java. Memory plays an important role in virtually any application, and it is important for you to learn how to utilize it properly.