Web services test code generator
Klaus Berg has recently released a test-code generator for JUnit-based Web service clients. If you're developing Web services using Axis2 and XMLBeans this wizard could turn your JUnit test client coding into a powerful code generation process. It also has uses for those using GUI-based testing tools like soapUI.

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Clearing resources

Alternatives to finalize() for clearing an object's memory resources?

QHow dependable is the finalize() method to clean all of the resources used by an object? Are there any different and more dependable methods for clearing the resources?

AFinalizers are fine for releasing an object's memory resources. For instance, if an object allocates a block of memory in a native method, it should explicitly free the block when the object is finalized, if not sooner. Finalizers are well-suited to memory reclamation because they are called as part of garbage collection, the goal of which is to free up memory.

If a program never comes up short of memory, though, the garbage collector might not run at all, so the finalizers would never execute. This means that finalizers shouldn't be used to free nonmemory resources like file descriptors or database connections. A well-designed class should include methods to explicitly free any such resources it allocates. A well-designed program will ensure these methods are called as soon as the resources are no longer necessary.

Bill Venners's Design Techniques column, "Object finalization and cleanup," covers this topic in detail (see Resources). The only point we could add to Bill's article is a discussion of the dangers of runFinalizersOnExit(), a method of java.lang.Runtime. Some developers use this method to ensure that every object's finalize() method will be called before the Java program exits. Unfortunately, it sometimes causes an object's finalize() method to be called before the object has been garbage collected!

Considering the potential dangers, you should at all costs refrain from using the poorly implemented runFinalizersOnExit() method.

Author Bio

Random Walk Computing is the largest Java/CORBA consulting boutique in New York, focusing on solutions for the financial enterprise. Known for their leading-edge Java expertise, Random Walk consultants publish and speak about Java in some of the most respected forums in the world.
Resources