The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Don't waste your time, please!!!

Posted by Kishori Sharan on December 02, 2000 at 12:16 AM

If you can't understand it then you are right, because there is nothing to understand in finalization of objects. Do you know, why? It's because Java Language Specification has clearly mentioned that there is no guarantee of time and order of finalization of objects. So obviously, it is waste of time on the part of a book writer as well as for a reader to explain and understand the order and time of finalization of objects in java.
Lets come to your questions. Let is no specific order when the objects in base class of when the objects in derived class will be reclaimed. To prove this point I have written a simple example below Test.java . Just run this example changing the total variable in finalize method of C2 class and watch the result. Change total from 10 to 100000000 in step of 10 times of previos value.
Second thing, when an exception ( Java says uncaught exception, but it happens even in a caught execption ) occurs in a finalize method when finalization of object is in progress then java ignores this kind of exception and the finalize method exits there itself. To prove it thru Test.java I am accessing str instance variable defined in super class C1 in the finalize method of C2. When you increase the totl value to around 100000 then System.gc ( ) will reclaim memory for str and then an exception will occurr when I am accessing str in C2 and then rest two messages won't be displayed from finalize method of class C2.
Lesson is, in finalize method of derived class do the processing and then at the end call the finalize method of super class. But, I am calling super.finalize ( ) as the first statement in finalize ( ) method of C2 which is causing the exception at run time.

Thanx
Kishori

///////////// Test.java

class A {
String s = "" ;
A ( String arg ) {
s = arg ;
System.out.println ( "Creating A:" + arg ) ;
}

protected void finalize ( ) {
System.out.println ( "Finalizing A:" + s ) ;
}
}

class C1 {
A a = new A ( "C1" ) ;
String str = "hello" ;
protected void finalize ( ) {
System.out.println ( "Finalizing C1" ) ;
}
}

class C2 extends C1 {
A a = new A ( "C2" ) ;
protected void finalize ( ) {
super.finalize ( ) ;
String temp = "" ;
int total = 10 ; // Change this value to 100 , 1000 , 10000
//and then run this program to see the result
for ( int i = 1 ; i <= total ; i++ ) {
temp = new String ( "Hello" + i ) ;
temp = null ;
}

// Try reclaiming the memory so that
// str in super class C1 is destroyed
System.gc ( ) ;

// Try accessing str. If str is already destroyed then
// it will throw an exception which will be
// ignored by Java and then this finalize will be terminated there

System.out.println ( "String str is " + str ) ;

// If exception was thrown then you cannot get this message
System.out.println ( "Finalizing C2" ) ;
}
}

public class Test {

public static void main ( String[] args ) {
System.runFinalizersOnExit ( true ) ;
new C2 ( ) ;
System.gc ( ) ;
}

}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us