Win a copy of Microservices Testing (Live Project) this week in the Spring 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

finalize() problem

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can finalize() be called more than once on an object? :roll:
 
vitthal wable
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reply Pls its urgent
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Urgent enough for you to try it yourself?
 
vitthal wable
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya, i know that we can call finalize() more thna once on single object but wthat about the statement that finalize() gets excecuted only once in life time here is code

class MyObject {
public void finalize () throws Throwable {
super.finalize();
System.out.println("finalize");
}
}

class Program {
public static void main(String[] args) throws Throwable {
MyObject obj = new MyObject();
obj.finalize(); //Line A
obj.finalize(); //Line B
System.out.println("Prepare GC...");
obj = null;
System.gc();

}
}

here it is running but how ???
 
author
Posts: 23925
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vitthal wable:
Can finalize() be called more than once on an object? :roll:



You may call finalize() as many times as you like, but the GC will only call finalize(), at most once. Basically...

When an object is eligible for garbage collection, the GC will call the finalize() method. Upon completion, it will check for eligibility again -- this is because the finalize() method can make the object reachable. If the object is still unreachable, or becomes unreachable later, the object is garbage collected. (the finalize() method is not called again)

Henry
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vitthal wable:
Reply Pls its urgent



http://faq.javaranch.com/view?PatienceIsAVirtue
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
You may call finalize() as many times as you like, but the GC will only call finalize(), at most once.



Not quite. The finalize() method may maintain a reference to 'this' which prevents garbage collection. After some time, garbage collection may occur again on the same object... ad infinitum.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:


Not quite. The finalize() method may maintain a reference to 'this' which prevents garbage collection. After some time, garbage collection may occur again on the same object... ad infinitum.



As repulsive as it might seem, the GC maintains a boolean flag for each object. Once it has called finalize() for an object, it will set the bit, and then never call finalize() on that object again, even under the circumstances you describe. Gross, I know, but them's the breaks.
 
Henry Wong
author
Posts: 23925
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:

Not quite. The finalize() method may maintain a reference to 'this' which prevents garbage collection. After some time, garbage collection may occur again on the same object... ad infinitum.



Tony,

Did you read the paragraph following the one you quoted? Yes, finalize() can prevent garbage collection, but it will never be called again by the garbage collector.

Henry
 
Henry Wong
author
Posts: 23925
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:

As repulsive as it might seem, the GC maintains a boolean flag for each object. Once it has called finalize() for an object, it will set the bit, and then never call finalize() on that object again, even under the circumstances you describe. Gross, I know, but them's the breaks.



I am actually torn with this. I actually like the fact that finalize() is called only once. The part that I kinda don't like is...

If they go through this touble to make sure it is only once, why can't they find a way to guarantee once (ie. prior to JVM exit, call finalize on every object that hasn't been called).

Oh well, now I am nitpicking...
Henry
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I first ran into this, many years ago, it just seemed crazy to me that you could exploit it to save an object from being collected once, but only once. If you could do it more than once, my thinking went, then you could use this as a kind of automatic object pool. Objects that are truly unused can hoist themselves back up into a pool, to be used again.

In retrospect, I'm pretty sure this would wreak havoc with the GC algorithm -- i.e., a pool that worked this way would be much slower than simply allocating new objects as needed under almost all circumstances.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

When an object is eligible for garbage collection, the GC will call the finalize() method. Upon completion, it will check for eligibility again -- this is because the finalize() method can make the object reachable. If the object is still unreachable, or becomes unreachable later, the object is garbage collected. (the finalize() method is not called again)


can you please elaborate on this with an example.

Thanks,Appreciate your valuable time.
[ September 18, 2006: Message edited by: Archies Gomes ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic