Tuesday, October 18, 2011

Checking when an Object is Destroyed in Java

As you may know, Java has no destructors like C++ or C#. Still, there is a method that allows you to check when an object is destroyed (or "collected") by the Garbage Collector (Java's mechanism to free up the memory that is no longer used by your program).

To check this, your class needs to contain a finalize method. The finalize method also needs to call the finalize method of its superclass (all Java classes inherit the Object class by default if there is no other class specified). Your code needs (or rather say it's recommended) to be included in the try block.
protected void finalize() throws Throwable 
{
        try
        {
            //TO DO CODE
        }
        finally
        {
            super.finalize();
        }
}
This method is useful if you want to check out the exact moment when a certain object is destroyed or if you want to keep track of how many object of a certain class exist in the program's memory.

If you want to signal when an object is destroyed:
protected void finalize() throws Throwable 
{
        try
        {
            System.out.println("Object destroyed of type" 
                             + this.getClass().toString());
        }
        finally
        {
            super.finalize();
        }
}
If your class has an toString method implemented the method could look like (you could see this way the specific object who was destroyed):
protected void finalize() throws Throwable 
{
        try
        {
            System.out.println("Object destroyed : " + this.toString());
        }
        finally
        {
            super.finalize();
        }
}
If your application is not console, you will probably need to replace the println method with a more "visual" method (which depends of you GUI framework, or the control in which you want to display the information).

If you want to keep track on how many object of a certain class exist in the program's memory:
public class MemoryTest 
{
      //Static counter variable
      private static int count = 0;
      //The object's id
      private int myId;
      //Constructor
      public MemoryTest() 
      {
        myId = count;
        count++;
        System.out.println("Object created" + myID);
      }
      //Finalize method implementation
      protected void finalize() throws Throwable 
      {
        try
        {
            System.out.println("Object destroyed " + myId);
        }
        finally
        {
            super.finalize();
        }
     }
}
According to the Java reference, an object is collected by the Garbage Collector when he is no longer referenced in the program. So, theoretically, he will be destroyed. Still, this rule does not always apply, because the Garbage Collector has a tendency to free up memory only when the program's memory is full.

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger