Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

garbage collection and finalize method- inconsistent result  RSS feed

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 23640
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anu Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Anu
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
author
Sheriff
Posts: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!