Local Tomcat memory profiling with VisualVM

I’m often using the excellent VisualVM tools to profile applications. It’s free (as in beer), delivered with the JDK and packed with enough tools to solve 99% of the performance problems. I have to give that credit to the authors they did an excellent job !

Today I tried to set up for a memory profiling of a Tomcat/Spring/Hibernate stack. The Tomcat instances didn’t show up in VisualVM’s "local" node. No problem. I’ll setup JMX to operate remotely and I’ll do it that way. Adding the properties to Tomcat’s …


… and restarted. Now I can connect to the Tomcat instance using port 3333.

I’m most often doing the profiling using the Sampler plugin. It’s fast and accurate enough for most profiling cases. While the "real" Profiler tool does code weaving to achieve a higher precision. (I guess.) And it takes a lot of time to patch a Tomcat with its applications deployed on.

The next problem is that the Sampler does not want to allow memory profiling for "remotely" connected JVM’s. CPU profiling is allowed however. I could do memory profiling with the JVM’s in the "local" node of VisualVM. So I have to make Tomcat visible again to VisualVM. I know that once worked !

In the end the solution is very straightforward: Set the environment variable CATALINA_TMPDIR to "/tmp". At least to the same value as the one of VisualVM. Once this is done, Tomcat pops-up again in VisualVM’s "local" node and you can do memory profiling using the Sampler plugin. I have no rational explanation (neither irrational for that matter) for this problem. At least now it works.

It seems to be an old problem however.

To make absolutely sure I’m not ever confronted with this I have put "export CATALINA_TMPDIR=/tmp" into a script in /etc/profile.d. Even if I re-install Tomcat, it will always work.

Hope this helps somebody too.

Published in: on April 20, 2011 at 15:15  Comments (8)  

The URI to TrackBack this entry is: https://javaheap.wordpress.com/2011/04/20/local-tomcat-memory-profiling-with-visualvm/trackback/

RSS feed for comments on this post.

8 CommentsLeave a comment

  1. Many many thanks for export CATALINA_TMPDIR=/tmp. I had problems that I had not seen Tomcat in under local node on linux machine.

    • you’re welcome 🙂 Took me a while to figure out myself…

    • Thank you as well.

  2. I’ve tried a few variations, but nothing seems to work on Windows for getting the Tomcat JVM to show up in the local node. I tried setting CATALINA_TMPDIR to \tmp and c:\temp and neither of those worked. Any suggestions? I’m running Tomcat with batch files.

    • Actually, I figured this out: as you pointed out in your article, the temp directory must be set to the one used by JVisualVM, which can be discovered from the VisualVM system properties tab (java.io.tmpdir). Once I set the CATALINA_TMPDIR directory properly, this worked beautifully. Thanks.

  3. Many thanks for the great help

  4. so basically if we want to do memory profiling on a remote box, we’re out of luck?

    • I’ve just tried to make sure of what I’m telling – but indeed. Remote applications only get the cpu profiling for the Sampler profiler.

      That being said, you’re not entirely defenseless. 🙂

      Monitor the remote JVM with the usual tools. When you think it’s having its memory leak, press the “Perform GC” button a number of times; to make absolutely sure the memory is not reclaimed. When that’s indeed not the case, ask the remote machine to dump its heap. That’s an hprof file the JVM will write on its local filesystem. That file can then be copied locally and loaded into VisualVM.

      Once you think the remote system is having its memory leak, you can request a heap dump.

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: