• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

finalize method

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Process {
public static void main(String[] args) throws Throwable
{
Test t = new Test();
t.finalize();
t=null;
Runtime rt = Runtime.getRuntime();
rt.gc();
System.out.println("Finalization Completed");
}
}

class Test {
protected void finalize() throws Throwable {
System.out.println("Finalize method invoked");
}
}
it is printing
C:\JAVA>java Process
Finalize method invoked
Finalize method invoked// From where this finalize method invoke second time and how?
Finalization Completed
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test t = new Test();
t.finalize(); //overload finalize by name so will call second time
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, Payal!
The finalize method is called at most once on every object once the object is eligible for garbage collection.
You call the finalize method yourself (not very beautiful, mind you...), and then you set your reference variable for the object to null. This means that there are no references pointing to that particular object, and hence it is eligible for garbage collection. This is noted by the jvm, which calls the finalize method on your object. That is why the finalize method is called twice.
Hope this helps!
//Kaspar
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to what Kaspar said here is what the JLS(12.6.1) has to say
"Because of the way that an object progresses from the unfinalized state through the finalizable state to the finalized state, the finalize method is never automatically invoked more than once by a Java virtual machine for each object, even if the object is again made reachable after it has been finalized.
Explicit invocation of a finalizer ignores the current state of the object and does not change the state of the object from unfinalized or finalizable to finalized."
-Alokesh
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As per messages in the thread, we know that finalize would be called just once on the object before the object is garbage collected.This is handled by JVM.
However, this doesnot prevent you from calling the finalize() method the way you did in the code snipplet you posted - t.finalize().This would produce the first output on your screen.
After that you are marking the object for Garbage Collection, by setting t = null, and then smartly (or should I say unsmartly, as it is not a smart idea to call Garbage collector..leave something for the JVM to do! ) and explicitly calling Runtime.gc().At this moment, you are just telling the JVM to garbage collect the object.
The JVM may oblige you by GC the object or may not; however, the finalize routine would certianly get executed JUST before the object is GCed.This is the reason you see the second statement outputed on your screen from the finalize() method.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 26, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic