aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection and finalize method- inconsistent result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection and finalize method- inconsistent result" Watch "garbage collection and finalize method- inconsistent result" New topic
Author

garbage collection and finalize method- inconsistent result

Anu Bhagat
Ranch Hand

Joined: Jun 20, 2008
Posts: 64
Hi,

I developed a small code to clarify my understanding of finalize method. I undersatnd it runs only once in an objects life.

my program print 112 or 121.
it looks like finalize is running on t1 again...which I think is not possible...so what is happening? please help me understand.



Thanks.

Anu


SCJP5.0, SCJA
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

Anu Bhagat wrote:I developed a small code to clarify my understanding of finalize method. I undersatnd it runs only once in an objects life.


However the API documentation says:
The finalize method is never invoked more than once by a Java virtual machine for any given object.


That must mean something different than "it runs only once in an object's life", then. Can you suggest what the difference might be?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
The reason why finalize() is running twice for t1 is because you are calling it explicitly from your code. The GC will only call finalize() once for each instance, but you can call it as many times as you want from your code. When you call it from your code that doesn't count as long as the GC goes. The GC will always call finalize() before reclaiming the memory for the object, and when it does, it will call finalize() exactly once.


All code in my posts, unless a source is explicitly mentioned, is my own.
Anu Bhagat
Ranch Hand

Joined: Jun 20, 2008
Posts: 64
Thanks.
Anu
Anand A. Parikh
Greenhorn

Joined: May 12, 2009
Posts: 21

Don't all methods of class Object have access modifier public?
In the above example how does
protected void finalize() { ... }

compile? and override the Object method?

Thanks,
Anand
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18821
    
  40

A Parikh wrote:
Don't all methods of class Object have access modifier public?
In the above example how does
protected void finalize() { ... }

compile? and override the Object method?

Thanks,
Anand


Please start new topics for new questions. First, it is kinda impolite to hijack another topic. Second, when you start a new topic, hopefully, you will create a new (relevant) subject which can be used by users to search with (in the future).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection and finalize method- inconsistent result