• 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

Garbage Collection

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have got a question regarding object finalization.

My Question is:
- When I set „tf = null“ why doesn’t it call the finalize method and print out
("Hey, I'm in TestFinalize finalize()")?
It seems it only calls the finalize() method when I set the reference to null
(tf = null) AND excplicitly suggest garbage collection with „System.gc()“.
WHY doesn’t it simply call finalize() method when i set the object reference
To null. Why do I have to call System.gc() additionally?

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object is unreachable, it becomes *eligible* for gc'ing - that is, the next time the garbage collector runs, it *may* gc the object. The finalize method is called *immediately* before the object gets gc'ed and there is no guarantee to when or wether this will happen besides that it will happen before an OutOfMemoryError occurs. Even System.gc() does only *suggest* that the gc gets activated.
Did that help?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic