Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

finalize() and cleanup

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am closing the file handle, DB Connections etc. in the finalize() method of a class. My program runs as a Windows service and the only place I could think of, to do cleanup is finalize(). Can some one tell me if thre is any other way of doing this? Thanks.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finalize() is not really recommended for performing clean up, because there is no assurance that the method will be invoked.

Instead of relying on the finalize method, it is better to free up the resources once after they are *finished*.

Nick
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that finalize() is not a good way to ensure that non-memory resources like DB connections are released. Your program needs to keep track of what it's using and release things explicitly when they are not needed.

The reason for finalize() being inappropriate is that it only gets called when the JVM decides that it wants to free some memory, by getting rid of unused objects. If there is plenty of memory, then this could happen a long time after the object actually becomes unused, or it might never happen at all.

One possible use for finalize() might be to assert that this releasing has been done. The assertion could then be followed by a clean-up method call. That way, development code (runs with assertions switched on) will alert the developer to any missing clean-up, but production code (runs with assertions switched off) will ensure that any missing clean-up will at least happen when the object is about to be GC'd.
 
Bala Krishna
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies. But, since my program runs as a Windows Service, (to be more specific, a thread that wakes up every minute, processes and goes back to sleep), I don't really know when I am done with the resources. finalize() seems to be the only option for cleanup().
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic