java resource management

Chong Yu Meng chongym
Mon May 17 11:58:48 PDT 2004


Net Llama! wrote:

>Greetings,
>I've got a question for the java experts.  I've got a java app running in
>j2ee (that i did NOT write).  Are there any good tools for doing resource
>management, such as detecting memory leaks, or memory usage optimzation?
>Basically I'm trying to determine if this java app can be optimized at
>all, or how wasteful it is with resources.
>
>  
>

Gah ! That's a tough job, Lonni ! Even if you had access to the source 
code, it's very difficult to fix these problems, although it's 
relatively easy to locate bottlenecks and (I suppose) memory leaks using 
Java Profiler software such as JProfiler 
(http://www.ej-technologies.com/products/jprofiler/overview.html).

Here's what I normally look for :

1. Since you're using Tomcat (I gather that from your previous emails), 
you'll need to determine if the code requires access to a database. A 
lot of programmers I know use the connection pool provided by the 
Jakarta group. Ensure that you are running the latest version ! I heard 
that there was a bug in the earlier version that might cause a memory 
leak or worse.

2. If the code uses straight JDBC connections, without a connection 
pool, ensure that all connections, statements, etc. are closed after 
they have been used (i.e. connection.close() ).The garbage collector in 
Java may not clear unused connections otherwise, and you may run into a 
situation where a lot of connections are left open or unreferenced, 
thereby clogging up memory, and when the garbage collector finally kicks 
in, you end up with an unresponsive application, that looks like it hung 
up. In my experience, most memory problems are caused by this. You can 
read more about garbage collection here: 
http://www-106.ibm.com/developerworks/library/j-jtp01274.html

3. Tomcat 4.x has a memory leak problem -- or so I have been told. If 
you can, upgrade to version 5 (but check that your application can still 
run in version 5!), where the admin tool apparently has some memory 
charts you can look at (actually, I'm not sure of this, only read it on 
the mailing list). If you don't want to upgrade, ensure that you restart 
Tomcat regularly (once a week? I guess it depends on the traffic). If 
you're really paranoid, then reboot.

4. You can play around with the amount of memory available to the JVM 
using different switches (i.e. adjust the heap size). A good write-up on 
this is here : 
http://www-106.ibm.com/developerworks/java/library/j-tip-heap-size.html.

My preferred method is 2. By closing all connections, you can actually 
realize some significant performance gains. This assumes that you have 
access to the source code and you know Java. Otherwise, I'd recommend 
either referring the problem back to the programmer, tweaking the heap 
size to get the optimum value or restarting Tomcat regularly. Adding RAM 
doesn't really work, because the heap size may be pre-determined for 
some Java VMs -- which means you'll need to tweak the switch value to 
get the optimum setting.

Hope this helps!

pascal chong






More information about the Linux-users mailing list