Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my tomcat application's memory usage keep growing?

My application is running on Windows Server 2000. The memory usage keeps growing (from 145m).

Is that normal? I am new to Java. The version is Tomcat5.5.

like image 673
user68461 Avatar asked Feb 19 '09 15:02

user68461


3 Answers

If it only ever grows, then that's a memory leak. But if it grows up to your max heap, then drops, then that is normal garbage collection behavior. There are many tools you can use to learn more. One of the simplest is to connect with JConsole (part of the JDK) and observe your heap over time.

You can also look at garbage collection information with various switches and parameters like -verbose:gc to start with.

If you want to diagnose memory leaks, there are a number of excellent tools available, including several free ones that work with Eclipse, NetBeans, IntelliJ, etc.

like image 113
Alex Miller Avatar answered Sep 21 '22 02:09

Alex Miller


The default behaviour of Sun "server" HotSpot is to enlarge the heap rather than clearing SoftReferences (used for caches). The default was to keep them for one second for every megabyte of maximum heap size available. So, expect the heap to grow to the maximum size. If you do actually run out of memory with OutOfMemoryException or performance becomes poor (due to excessive GC or small caches), then you need to look for memory leaks.

Also Tomcat servers often suffer from memory leaks after reloading applications. For instance, Tomcat shares threads between all applications, which often causes Sun's ThreadLocal implementation to improperly retain values.

like image 25
Tom Hawtin - tackline Avatar answered Sep 19 '22 02:09

Tom Hawtin - tackline


IMO Tom Hawtin's answer is the best. It grows until it hits the max, then runs GC. In a server environment this makes sense: You want the best performance, not best memory usage. You pre-calculate the total amount of memory, then give each app a maximum and then everything fits and has the best performance. This behavior can be tweaked.

Use jconsole to look at how much is really being used. Do GC and watch what it goes down to. That number should not grow over time or you have a memory leak. Use visualvm to debug a memory leak.

Each time you reload an application it uses additional Perm Gen memory which cannot be reclaimed (in Sun JVM, others like JRockit don't have this problem). In production you shouldn't be reloading the app. Restart Tomcat each time. If you really want to keep doing it then you can increase the max memory and also increase the Max Perm Gen memory with the flag -XX:MaxPermSize=256m

like image 45
Sarel Botha Avatar answered Sep 20 '22 02:09

Sarel Botha