Sunday, September 26, 2010

Cleanup: finalization and garbage collection

The garbage collector only knows how to release memory allocated with new.
To handle this case, Java provides a method called finalize( ) that you can define for your class. Here’s how it’s supposed to work. When the garbage collector is ready to release the storage used for your object, it will first call finalize( ), and only on the next garbage-collection pass will it reclaim the object’s memory. So if you choose to use finalize( ), it gives you the ability to perform some important cleanup at the time of garbage collection.

1.Your objects might not get garbage collected.
2.Garbage collection is not destruction.

What it means is that if there is some activity that must be performed before you no longer need an object, you must perform that activity yourself. Java has no destructor or similar concept, so you must create an ordinary method to perform this cleanup.

3.Garbage collection is only about memory.

No comments:

Post a Comment