jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Java-Mind-Teaser: Memory Problem with Tomcat/JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java-Mind-Teaser: Memory Problem with Tomcat/JSP" Watch "Java-Mind-Teaser: Memory Problem with Tomcat/JSP" New topic

Java-Mind-Teaser: Memory Problem with Tomcat/JSP

Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61

Hello, Java Gurus,
I have a JSP program which uses JavaBean to process data from
a database and outputs the result. I am running into
some memory problems. When I run the JSP program through
Internet Explorer, I usually monitor the memory used by
the process "java.exe". My memory problem is related to
the process "java.exe". Here is the detail:
Sometimes, because of the data returned from the database,
the JSP program will crash. When this happens, depending
on the data being processed, the process "java.exe" may
take quite large amount of memory (say 400k). I would
assume that since the JSP program has crached, garbage
collector will sneak out to mop the space once in a while.
But in my case, this never happens. If I rerun
the same JSP program again without restarting Tomcat,
then the memory taken by this process "java.exe" will start
to increase from where the previous crash left, i.e., 400k.
Of course, the same program will crash again, leaving
the memory taken by process "java.exe" to be about
doubling the previous amount, say 800k. This could
quickly clog the machine's memory and crash the webserver
(in my case, Tomcat).
Of course, if I stop Tomcat and then restart after each
jsp crashes, there will be no memory problem described
My question to all you Java Jurus are:
(1) Why would this happen?
(2) How could I fix this, i.e., how could I ensure that
whenever a JSP program crash, the memory taken
by itself will be available for garbage collection,
or be collected by the system (without stopping Tomcat)?
I would appreciate any commects, suggestions, etc.

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
When you say the JSP program crashed, what do you really mean? The servlet representing a JSP runs in the context of a server - Tomcat in this case - if the servlet throws an exception, and you the programmer don't handle it correctly, there could be all sorts of objects left in memory - Database connections, objects attached to a session, etc.
How are you handling exceptions?
Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61
Hi Bill,
Thanks for the reply. I used the word "crash" in a very
loose way. The program doesn't actually CRASH. In fact
it just hangs in there. There is not exception issue here
since any potential exception issues have been handled.
The problem is "out of memory".
Specifically, the database is huge. Without taking
additional step to limit the size of returned data,
the JavaBean just issues a query and get whatever
returned data in the ResultSet. To give you an idea of
the size of this returned data, in one instance, it
returned 999,000 entries of data. I could run this
query once successfully and watch the memory taken
by process "java.exe" to grow to 55,000k (!). Run the
same program again and the memory will grow to 74,000k.
Run the same program the third time, then there will
be not output, nothing, the program hangs in there
and on Tomcat console, it gives you an error message,
something like "out of memory". So my problem is
not CRASH, it is out of memory.
I have taken precaution to make sure that my JSP page
has a page scope. I would assume that every time I
re-execute the same program, the memory taken by
previous execution will be available for garbage
collection, and will be garbage collected as JVM
detects the need of more memory. Apparently this
is not working in my case.
Any thoughts?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
What a monster of a database query!
You are going to have to figure out a way to take smaller chunks of the search result at a time. Unfortunately I don't do a whole lot of JDBC so I can't say how to do that.
I agree. Here's the link: http://aspose.com/file-tools
subject: Java-Mind-Teaser: Memory Problem with Tomcat/JSP
It's not a secret anymore!