Showing posts with label destructor. Show all posts
Showing posts with label destructor. Show all posts

Sunday, 15 May 2011

Object destruction and the finalize method

The aim of destructor in any OOPs language is:

  1. Free up the memory (c++ suffer from memory allocation / deallocation)
  2. Clean up any other resources (like closing of open file stream)

Java take cares of all and hence there is no destructor in JAVA. (With the help of Garbage collection) but still if you want to perform some additional tasks, then you can use the finalize() method of java.

But, we can’t rely on finalize() as it depends on GC. Sometimes, GC may never be invoked during the lifetime of the program. A good idea is to implement a method, say cleanUp() which does, any special processing required, ofcourse, other than freeing up memory. The programmer should call this method at the approppriate place in the program. That is the only way to make sure your program works correctly.

Example code of finalize in java:

class HasFinalize{

public static int number_of_things = 0;
public String what;

public HasFinalize(String what) {
this.what = what;
number_of_things++;
}

protected void finalize () {
number_of_things--;
}
}

public class TestFinalize {
public static void main(String[] args)
{
Thing obj = new Thing("Test App");

}
}


The method call System.runFinalizersOnExit(true) guarantees that finalizer methods are called before Java shuts down. However, this method is inherently unsafe and has been deprecated. An alternative is to add "shutdown hooks" with the method Runtime.addShutdownHook—see the API documentation for details.

 

If a resource needs to be closed as soon as you have finished using it, you need to manage it manually. Supply a method such as dispose or close that you call to clean up what needs cleaning. Just as importantly, if a class you use has such a method, you need to call it when you are done with the object.

Wednesday, 1 September 2010

Deterministic lifetime of object in c++ as compared to java

C++ approach of Object Destruction

Some object-oriented programming languages, notably C++, have explicit destructor methods for any cleanup code that may be needed when an object is no longer used. The most common activity in a destructor is reclaiming the memory set aside for objects. Because Java does automatic garbage collection, manual memory reclamation is not needed and so Java does not support destructors.
Of course, some objects utilize a resource other than memory, such as a file or a handle to another object that uses system resources. In this case, it is important that the resource be reclaimed and recycled when it is no longer needed.
So cpp follows deterministic approach of object destruction.

Java Approach of object destruction

Java's approach is automatic garbage collection. You can add a finalize method to any class. The finalize method will be called before the garbage collector sweeps away the object. In practice, do not rely on the finalize method for recycling any resources that are in short supply—you simply cannot know when this method will be called. So java follows non-deterministic approach.

Advantage of Garbage collection

See here for advantage of garbage collection.

Disadvantage of Garbage collection

See here for disadvantage of Garbage collection

 

Final note

Garbage collection has increased problem, rather than solving many. Due to limited memory, we can get out of memory error. But this problem or disadvantage, we have to live with java.