• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Garbage Collection

 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have one query in garbage collection concept, in K&B book below statement is given.

"If first time an object�s finalize() method runs and the object is saved from the Garbage Collection, then the second time that object is about to be Garbage Collected, finalize() will not run".

My query is, if we call finalize method in our code, and assume that same object is about to be garbage collected, finalize() will be called again by garbage collector?.
[ October 02, 2008: Message edited by: ramesh maredu ]
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Finalize will be called only once.

Suppose in Finalize method, you reassign a reference to the same object, so that object will be saved from Garbage collection.

And the next time the same Object is garbage collected, the Object's finalize wont run.
[ October 03, 2008: Message edited by: chander shivdasani ]
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My query is, if we call finalize method in our code, and assume that same object is about to be garbage collected, finalize() will be called again by garbage collector?.

If i get your question correctly, we have a method say m1 in a class which calls finalize() method. That method is called once and the object is available for GC. Does Garbage collector calls finalize() method or not?

According to me, it will call finalize() method. As the stmt which is given below is about garbage collector calling finalize() method.

"If first time an object�s finalize() method runs and the object is saved from the Garbage Collection, then the second time that object is about to be Garbage Collected, finalize() will not run".

But i am not sure.Correct me If i am wrong!
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it doesn't matter if you call finalize() method yourself in the code.It will be as good as any other method in an object. But when GC calls finalize method on an object it will never call it again on the same object.
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srilatha, you got me correct.

Thanks all, so if we call finalize() method on our object it will not be called again by the garbage collector.
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so if we call finalize() method on our object it will not be called again by the garbage collector



No..this is not correct!
If finalize() method is called by garbage collector once, it wont be called by garbage collector again. Even if we call finalize() method directly from our code, garbage collector may/may not call finalize() method before GC takes place. But we cannot say that finalize() method wont be called again by garbage collector.

I hope this will clear your doubt!
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If finalize() method is called by garbage collector once, it wont be called by garbage collector again. Even if we call finalize() method directly from our code, garbage collector may/may not call finalize() method before GC takes place



Thank you, this explanation answers my question well.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the rule is that the garbage collector will always run finalize on the objects it garbage collects. also it will call it once on every object not more than that. It has nothing to do with how many times you have called finalize on an object. The only case that finalize will not run on a object is when the JVM exits before the object is garbage collected...
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic