aspose file tools*
The moose likes Java in General and the fly likes finalize() problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "finalize() problem " Watch "finalize() problem " New topic
Author

finalize() problem

vitthal wable
Greenhorn

Joined: Sep 09, 2006
Posts: 16
Can finalize() be called more than once on an object? :roll:
vitthal wable
Greenhorn

Joined: Sep 09, 2006
Posts: 16
Reply Pls its urgent
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Urgent enough for you to try it yourself?


Tony Morris
Java Q&A (FAQ, Trivia)
vitthal wable
Greenhorn

Joined: Sep 09, 2006
Posts: 16
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
Sheriff

Joined: Sep 28, 2004
Posts: 18714
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by vitthal wable:
Reply Pls its urgent


http://faq.javaranch.com/view?PatienceIsAVirtue


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18714
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18714
    
  40

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jun 19, 2005
Posts: 34
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize() problem