Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize method

 
praneeth kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone

Its given in K&B that finalize method will be called only once and the second time,the object will be GCed directly.Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes even than finalize method is called only once
 
praneeth kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the reply.

Is there any way to check this practically?

Thank You.
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont have the code but their are refrences(phantom refrence etc) which can be obtained before the object is GC for some proccessing need to be done before the object is destroyed you can try this.

And this is the reason finalize is not called twice it'll turn up into an infinite loop.

object destroy -- finalize call --- obtain refrence----do something --- destroy object -- finalize call --- so on.
 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praneeth kumar

Its given in K&B that finalize method will be called only once and the second time,the object will be GCed directly.Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?



if you invoke finalize method explicitly on any object, finalize will be invoked then also on that object by JVM. But JVM invokse only once on that object. If you try to resurrect that object through finalize, and when again that object becomes elligible for garbage collection, this time JVM wont invoke finalize() on it.

here is a sample code::



somehow System.gc() did perform garbage collection (atleast on my system) and the O/P I got::

E:\Priyam>java Test
102010

Though I have not resurrected the object, but you can try your self...

[ July 23, 2007: Message edited by: Priyam Srivastava ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy ranchers!

Praneeth asked:
Is it the same even if we invoke finalize method explicitly?In other words,if we explicitly invoke finalize method,will it be invoked again before being GCed?


No it is different.
And yes, the finalizer daemon may invoke it again.

The GC will call finalize only once (Maximum. Maybe never.). How many times you invoked finalize directly does not matter.
Try to design some example code to explore.



Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic