Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize() problem

 
vitthal wable
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Reply Pls its urgent
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urgent enough for you to try it yourself?
 
vitthal wable
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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 ???
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic