tomcat-users mailing list archives

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

Howdy,

>You shouldn't put anything in your finalize() method, it's a bad
practice
>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.clear();
    aBigMap = null;

    aBigSet.clear();
    aBigSet = null;
 
    aBigList.clear();
    aBigList = null;

    // Always call super.finalize()
    // at the end of custom finalizers.
    super.finalize();
  }
}
// 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
this
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: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message