• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Destructors

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could anyone clear my doubt :
Does Java have destructors?If not then is Garbage collector an alternative for that?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most common usage of destructors is to free memory - and java will do that for you automatically via "garbage collection"

That said, if you have some other special resource to free or if you're curious as to when the GC runs, implement a finalize() method (protected void finalize()) which will be run when the GC is about to delete the object.

It's important to bear in mind that the finalizer is run when the GC frees your object, not when the object first becomes garbage (i.e. unreferenced) - depending on memory conditions inside the heap on your specific platform and the particulars of the runtime you use, it could be a very long time before the finalizer is called during a GC cycle.
 
kt randhawa
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinu Nanduri:
The most common usage of destructors is to free memory - and java will do that for you automatically via "garbage collection"

That said, if you have some other special resource to free or if you're curious as to when the GC runs, implement a finalize() method (protected void finalize()) which will be run when the GC is about to delete the object.

It's important to bear in mind that the finalizer is run when the GC frees your object, not when the object first becomes garbage (i.e. unreferenced) - depending on memory conditions inside the heap on your specific platform and the particulars of the runtime you use, it could be a very long time before the finalizer is called during a GC cycle.



Thanks.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cleanup: finalization and garbage collection
 
reply
    Bookmark Topic Watch Topic
  • New Topic