tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject RE: Release of memory
Date Fri, 03 Jan 2003 18:44:22 GMT


On Fri, 3 Jan 2003, √ėyvind Hvamstad wrote:

>
> However, what does tomcat do after the "service" method returns? The
> Servlet interface has a "destroy" method, is it used? Why doesn't Tomcat
> release the servlet instance? Can anyone explain the reason?
>

Because the theory of a servlet container is that you're going to get
*lots* of calls to your servlet, so why should the container waste the
time to init and destroy it on every single call?  For maximum
performance, you want it to be loaded and initialized once, and then just
the service() method called for every request.

Tomcat will call the destroy() method when you take the application out of
service -- either because you remove it or because you're shutting the
server down.

If you are calculating something that takes a lot of memory, and it
happens on every request, you should store the reference to that object in
a local variable inside the service() method instead of initializing it in
init().  This will also likely avoid thread safety problems when you start
getting more than one request at a time (there's only one copy of instance
variables in a servlet, because there is only one instance of the servlet
class being loaded).

Craig


--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message