Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

finalize() method doubt........

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

There is a guarantee that finalize() method will run one and only once....

Is there any gaurantee that finalize() will ever run during the lifetime of the object...

OR

U can say, will finalize() always run once, whenever its been deleted by garbage collector no matter sooner or later.

Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At most once (0 or 1), you can't know for sure, that your object will ever be garbage collected.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you can't know for sure, that your object will ever be garbage collected.



Say If it is garbage collected, then will finialize() run once for sure??
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hardik,
If its garbage collected. finalize() method may run or not but if it runs it will be only once for the particular object.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
------------------------------------------------------------------
Say If it is garbage collected, then will finialize() run once for sure??
-----------------------------------------------------------------------


We cannot say wheather finialize() method will run or not.

Because if the garbage collector call's the finialize() method on particul
object ,and in the finialize() method if you pass the reference of the object to some method,and the next when garbage collector will not call finialize() method on that object

Thanks
Anil Kumar
[ May 14, 2007: Message edited by: anil kumar ]
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In first attempt to garbage collect some object, finalize is called. It is possible to ressurect object which is going to be garbage collected. If so, the second attempt to garbage collect will not call finalize again.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B]But here john and nik are given different answers...[/B]


nik : If its garbage collected. finalize() method may run or notbut if it runs it will be only once for the particular object.

john : In first attempt to garbage collect some object, finalize is called.



See friends, I am pretty clear that finalize() will run atmost once,
all I want to know is that, will it run atleast once when garbage collceted
[ May 14, 2007: Message edited by: Hardik Raja ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i know, yes, it will run atleast once if the JVM attempts garbage collection.
But since you cannot guarantee that the object 'will' be garbage collected, since garbage collection cannot be forced, so there is no guarantee that finalize() will run even once.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garbage collector would run when JVM feels that the memory should be collected. It is quite possible that the JVM has ample amount of memory available and hence the Garbage collector never run.

What if the garbage collector runs. Then again if the object is being garbage collected for the first time then the finalize method would be called otherwise it wouldn't be called. But there is no gaurantee that even if the Garbage collector runs all objects eligible for garbage collection would be garbage collected. So again it's not necessary that the object's finalize() method would be called.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,

The below is one of my prev replies.

See friends, I am pretty clear that finalize() will run atmost once,
all I want to know is that, will it run atleast once when object is garbage collected



Its very good that all users are give detailed information about this problem but I just wanted to know whether the above quote's answer is YES/NO

So I request any senior programmer or a Moderator to just give their comment
on the issue as I am still confused...

Also this doubt arrised from a point from two minute drills from K&B book.

"1. The finalize() method is guaranteed to run once and only once before the
garbage collector deletes an object.

2. The garbage collector makes no guarantees, finalize() may never run."
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HR: "1. The finalize() method is guaranteed to run once and only once before the
garbage collector deletes an object.


When an object is about to be deleted then yes the finalize method would run and that too only if it hasn't run before. But again there is no gaurantee that the JVM would always delete all Objects. But yes if it is about to be deleted then for the first time the finalize method would run.

HR:
2. The garbage collector makes no guarantees, finalize() may never run."


If the object is about to be deleted the finalize would certainly run. But as I said before there is no gaurantee that JVM would actually delete an Object. The point is that if an Object is being deleted the finalize would certainly run.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Anupam...

Thanks a lot. Got my doubt cleared.


Regards,
Hardik.S.Raja
Preparing for SCJP
 
permaculture is largely about replacing oil with people. And one tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic