Tuesday, January 30, 2007

Deterministic destruction of Objects in Java - An idea

I love programming in C++, despite the hues and cries against it as a "most programmer unfriendly" language. I have my own reasons to like C++. Top two reasons being:
  • Bjarne was born in 1950, it was the year when my father was born.
  • Initial C++ (C With Classes) was released in 1979, it was the year when I was born. :-)
Nevertheless, there is one aspect of C++ that I find very useful and will definitely help prevent resource leaks. It is the destructor of an object. In simple terms, whenever we exit a lexical block (a block enclosed within "{" and "}"), all the objects constructed within that block are guaranteed to be destructed. The guarantee is true even when the exit happens due to thrown exceptions.

This guarantee is a very strong weapon for any programmer. It makes destruction of an object a deterministic phenomena. Objects created in heap using new key word require the user to explicitly destroy them, and thats not what I am discussing here.

One of the good programming practice is to acquire and release any resource within the same lexical scope, as much as possible. (I hear you yelling "Its not always possible, fella" and I agree with you!)

If we look at the object life cycle models provided by C++ and Java, C++ implicitly supports the above mentioned practice. But in Java, every user of the object must remember to invoke the destructor's equivalent (freeResources, resetAll, etc). finalize method in Java is altogether for a different purpose, and the language inventors themselves strongly discourage using and relying upon finalize method.

I was chewing this idea for sometime and this is what I feel.
Garbage collection and object destruction are two different things. Whenever a lexical scope exit happens, the JVM has the list of objects within that scope. JVM can decide if an object still has any references to it, or it is safe to be destructed. Thus destructed object can wait in heap till GC occurs.
My thought is not too deep to be incorporated into a production release! But it certainly is worthy enough to be considered.

Is there a rationale behind not supporting deterministic object destruction in Java?

3 comments:

Harris The Virus said...

Being a java programmer, its my duty to defend java. So here it is.

-Java has the garbage collection feature jus to ensure no memory leaks occur.
-I think in Java there is way to explicitly call the Garbage collection. So no one waits :)
-Introducing destructors concepts in Java will lead to memory leaks and mismanaged objects. This when creating large enterprise softwares will be a huge overhead.


These are just my ideas. So no hard feelings towards C++. C++ is still capable of doin so many things Java cannot do.

Jolly Roy said...

To Harry: My thought was not to remove GC. But to make object destruction a deterministic one. Of course, I love the availability of GC in Java. And I tend to disagree that "no one waits for GC", as no one explicitly invokes GC either (at least in the code that I have seen). And I don't see any guarantee from System.gc() call that GC will happen, it is only a "best effort" call and not a guarantee.

Harris The Virus said...

System.gc() is a best effort call and not a guarantee call. But incorporating object destruction methods for java will mean that there will be more memory leaks. And since object destruction will be dynamic, it will mean more and more debuggin to find "bad references".

Though GC maynot be the best way to handle "dead" objects, its sure is a good effort to ease the programmer. Maybe a new GC design will be the solution. ;)