You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Michael Nicholson <ma...@email.unc.edu> on 2002/08/29 15:07:58 UTC

OT: Garbage Collection in Java: Am I an idiot?

The answer to the (completely rhetorical) question is probably yest, but I've got another question, too.

So, I have this moderately large application with jsps, servlets, and an Oracle DB.  It does its thing ok, but it never releases memory.  There are certainly session variables (probably way too many, but that's (hopefully) a question for another day), but even after the session expires, I'm still using the same amount of memory.  I even included a direct call to System.gc() on my first page, hoping to collect the memory from the three day old sessions, but no luck.  

So, my thoughts/questions:  Some connections are still opened (this is what's running on my development server right now: however, the actual development is done in Sun ONE on my desktop, so I'm going through and making sure all of those are closed there... but I'm also trying to debug this memory issue), so maybe these open connections force some of the session scoped beans to persist?  Forever?  Or am I just missing something completely about garbage collection?

So, in short, if I have a session scoped bean that has in it something like a connection or a recordset (which is most likely not a good idea... I now know that), even after the session times out, would that bean continue to exist?  And eat up my memory?  

Sorry for the rambling.

Michael Nicholson,
Carolina Center for Public Service

Re: OT: Garbage Collection in Java: Am I an idiot?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 29 Aug 2002, Michael Nicholson wrote:

> Date: Thu, 29 Aug 2002 09:07:58 -0400
> From: Michael Nicholson <ma...@email.unc.edu>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: Tomcat Users List <to...@jakarta.apache.org>
> Subject: OT:  Garbage Collection in Java:  Am I an idiot?
>
> The answer to the (completely rhetorical) question is probably yest, but
> I've got another question, too.
>
> So, I have this moderately large application with jsps, servlets, and an
> Oracle DB.  It does its thing ok, but it never releases memory.  There
> are certainly session variables (probably way too many, but that's
> (hopefully) a question for another day), but even after the session
> expires, I'm still using the same amount of memory.  I even included a
> direct call to System.gc() on my first page, hoping to collect the
> memory from the three day old sessions, but no luck.
>

One thing you should understand about Java JVMs is that they don't give
any allocated memory back to the operating system, under any
circumstances.  The allocated memory will be in the JVM's heap space (and
available to Java classes to use), but once you give memory to Java, it's
gone ...

You can use JVM configuration properties to control this memory allocation
-- just add whatever options you need to an environment variable called
CATALINA_OPTS before starting Tomcat 4.

> So, my thoughts/questions:  Some connections are still opened (this is
> what's running on my development server right now: however, the actual
> development is done in Sun ONE on my desktop, so I'm going through and
> making sure all of those are closed there... but I'm also trying to
> debug this memory issue), so maybe these open connections force some of
> the session scoped beans to persist?  Forever?  Or am I just missing
> something completely about garbage collection?
>
> So, in short, if I have a session scoped bean that has in it something
> like a connection or a recordset (which is most likely not a good
> idea... I now know that), even after the session times out, would that
> bean continue to exist?  And eat up my memory?
>

When the session was invalidated (or timed out), all of the session
attributes will have been removed from the session, but they will continue
to exist (from the JVM perspective) until they are garbage collected --
and, if there are any other live references to those beans anywhere (which
seems quite likely in your scenario; for example, the JDBC driver will
undoubtedly have references to open result sets), then garbage collection
will *never* happen.

The most common strategy for dealing with this is to make your session
attribute beans implement javax.servlet.http.HttpSessionBindingListener,
so that they get notified when they are removed from the session and can
clean themselves up.

> Sorry for the rambling.
>
> Michael Nicholson,
> Carolina Center for Public Service

Craig



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>