Short Note On Application Performance Tuning

We discuss the following basic points in this post- Heap, Heap Dumps and Thread Dumps, Memory Leaks and Connection leaks and Connection pooling
What is java heap?
When new java objects are created they are allocated memory in the java heap. Once they are no longer referenced by other java objects they may be garbage collected and memory is returned to the heap. Xms and Xmx are java switches that allow you to manipulate memory allocated to the heap.
There are two different types of heap – the new generation heap where new objects are allocated memory and the old generation heap that accommodates objects that have been in memory over a longer period. Perm memory is memory allocated to objects that have been loaded into memory through the classloader and will be removed from the perm memory only when the classloader is garbage collected.
OuOfMemory errors can be due to too many large objects in the session object in a web application and when there is not enough memory to create new objects in the java heap. This may occur when java objects are not returned back to the heap by the garbage collector. OutOfMemory exception may occur due to memory leaks. Memory leaks can be debugged by using the command:

java -verbose:gc

which will provide information on the garbage collector, objects garbage collected, etc and is used to fix garbage collector problems. A memory leak may be fixed by analyzing the logs from the above command.
Calls to Runtime.gc or Sytem.gc() should not be explicitly made. Note that threads including the JVM are paused when the major GC runs. Minor GCs do not cause the JVM to pause, but to some extent it is okay, as long it doesn’t cause noticeable slowness.
Java heap dump and thread dumps can be used to produce a stacktrace of the heap/ running threads. The stacktraces look similar to an exception stacktrace. Connection leaks are more difficult to detect since we may not know exactly where in the code a certain connection was not closed. This may be analysed by generating heap/ thread dumps using use case by use case.
Sometimes a memory leak/ application slowdown / hangup, may be difficult to find after it has been fixed. In that case one should enable GC logging / heap/ thread dumps tracing so that the next time this happens the cause is correctly found.
If a thread dump shows too many threads running it may indicate a reason for application slowdown. Thread dumps are mostly to find what the JVM is busy doing. If we see an unusually high number of threads such as more than 300 or so that means the JVM is working too hard on something and then you should look at what these threads are doing. If you see a lot of threads stuck on reading from the db that indicates db slowness. If you see too many threads servicing Servlet requests that means the JVM is overloaded with too many users.
Sometimes it helps to take thread dumps a few minutes apart and compare the two thread dumps to see if threads are moving along or stuck. If thread A in dump1 is doing the same thing in dump2 taken later, then it’s taking too long; usually you should be able to see it moving along and do different things.
It’s ok to have long running threads as some threads are by design supposed to be long lived e.g. you can have a pool of threads to consume JMS messages. Those threads will be alive for a long time and will be reused over and over again. So that’s normal as long as they don’t hold big objects for a long time, it’s not a memory leak.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: