tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <>
Subject [OFF-TOPIC] RE: Is it ok to close connection in finalize() ?
Date Fri, 21 Feb 2003 14:29:17 GMT


>You shouldn't put anything in your finalize() method, it's a bad
>because it can cause issues with the jvm's gc.

I strongly disagree with the above statement.  Finalize methods have
their use -- that's why they're around.  I agree with your specific
answer to this guy's connection handling question -- finalize() is not
the place for that.  

But finalize() can be useful for many other things.

Consider the following class:

Class MyClass {
  private Map aBigMap;
  private Set aBigSet;
  private List aBigList;

  // This class does various
  // things where the member structures
  // can grow very very large and consume
  // significant memory

   * Finalize: clear member
   * structures.
   * @throws Throwable
  protected void finalize() throws Throwable() {
    aBigMap = null;

    aBigSet = null;
    aBigList = null;

    // Always call super.finalize()
    // at the end of custom finalizers.
// End of class: MyClass

The above custom finalizer will result in much more efficient memory
claiming by the garbage collector, almost independent of collector
implementation (parallel, mark and sweep, concurrent will all benefit
from this).  Moreover, the finalize() method is the best place to do
sort of cleanup as you are assured the data won't be needed again.

Yoav Shapira
Millennium ChemInformatics

This e-mail, including any attachments, is a confidential business communication, and may
contain information that is confidential, proprietary and/or privileged.  This e-mail is intended
only for the individual(s) to whom it is addressed, and may not be saved, copied, printed,
disclosed or used by anyone else.  If you are not the(an) intended recipient, please immediately
delete this e-mail from your computer system and notify the sender.  Thank you.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message