Profile Tomcat Application With VisualVM
Asked Answered
B

4

61

I would like to profile an application that runs on Tomcat with the VisualVM tool. Unfortunately, when I tell VisualVM to profile Tomcat, Tomcat prints out a couple of messages indicating it is connecting to a profile tool and then simply exits.

Details…

I am using Windows XP, Tomcat 6, VisualVM 1.2.1, and JDK 1.6.0_11.

  1. In Visual VM, I right-click on the Tomcat Application and select “Profile”
  2. In the Profiler Tab, I press the Memory button (or the CPU button).
  3. Tomcat exits

Note that if I right-click on the Tomcat Application and select “Heap Dump” that seems to work OK.

Boldfaced answered 11/12, 2009 at 19:44 Comment(3)
Is anyone using VisualVM to profile Tomcat applications?Boldfaced
I don't see a "profile" tab when connection via JMX or JStatd. Do I need special versions of java or jvisualvm?Amazing
I also don't see a "profile" tab when connecting via JMX. Did this issue get resolved for others?Arterial
B
88

I have VisualVM profiling working with my Tomcat application now. I needed to add the following parameters to the tomcat startup:

-Dcom.sun.management.jmxremote.port=8086
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

Here is a nice article on monitoring Tomcat with VisualVM.

Boldfaced answered 16/12, 2009 at 16:27 Comment(4)
Thanks I am able to see the objects created, etc in the visual VM. I doubt we have a memory leak in the application and I want to see which object is holding the references of what objects ? + which objects are taking how much of memory? Is it tomcat's object or its application's object. I want to track down till code level is it possible? Is it possible to attach my code somehow with visualvm? Thanks in advance.Retro
That's not profiling. That's sampling. I have a JMX connection but only CPU sampling is available. How do I get profiling (instrumentation + memory) available with tomcat6.exe ?Audly
recently configured with linux, if smd needed progrnotes.blogspot.com/2012/07/…Gatias
@Audly I just ran into this. I discover that if I stop the Tomcat6 service and run Tomcat6.exe as a command-line app, VisualVM is able to connect to it as a local JVM and memory profiling is available.Annelieseannelise
C
7

Yes we do profile Tomcat applications.

Go to catalina.bat or catalina.sh and this to your JAVA_OPTS (I am using Tomcat 6.0.16):

-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Your JAVA_OPTS should look like

set JAVA_OPTS=%JAVA_OPTS% -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties" -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

Updated after Ryan comment that it is better to use setenv.sh. This is my setenv.sh for JDK 8. Missing few other settings but good to start with.

SUN_JVM_OPTS="
    -server \
    -XX:MaxMetaspaceSize=3G \
    -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled \
    -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 \
    -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark \
    -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=3 -XX:GCLogFileSize=2M \
    -XX:+HeapDumpOnOutOfMemoryError \
    -Dsun.net.inetaddr.ttl=60 \
    -Dcom.sun.management.jmxremote  \
    -Dcom.sun.management.jmxremote.port=8480 \
    -Dcom.sun.management.jmxremote.authenticate=false \
    -Dcom.sun.management.jmxremote.ssl=false"


# Set custom application options here
APPLICATION_OPTS="-Dlog4j.configurationFile=patht-to-log/log4j2.xml -Dlog4j.debug=true "

JVM_OPTS="$GENERAL_JVM_OPTS $SUN_JVM_OPTS"
CATALINA_OPTS="$JVM_OPTS $APPLICATION_OPTS"
echo "Tomcat started with settings "$CATALINA_OPTS

Once you drop the setenv.sh in bin directory, you can see the changes in console on startup.

Here's another step by step tutorial to profile Tomcat applications with Visual VM: Trouble shooting application performance with Visual VM

Cozenage answered 19/9, 2011 at 14:37 Comment(3)
If I follow the description on the link I can connect VisualVM to tomcat but there is no profile tab. Also, the description is not exact. It says "Click on Local or remote" but there is no such selection in VisualVM. I chose "Add JMX Connection" and specified a "localhost:9090" connection. I guess that's what they mean...Ostraw
You should really not use JAVA_OPTS for this - you should use CATALINA_OPTS instead in a bin/setenv.sh file (you shouldn't edit catalina.sh - it already calls out to setenv.sh for you)Probst
Agree. Updated the answerCozenage
B
2

I am using Tomcat 7 and the full configuration reqires more parameters to work.

-Dcom.sun.management.jmxremote=true 
-Dcom.sun.management.jmxremote.port=9090 # port to connect JMX 
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Djava.rmi.server.hostname=50.112.22.47" # IP of the server running tomcat (it is necessary)

source: http://blog.markshead.com/1129/connecting-visual-vm-to-tomcat-7/

Bullwhip answered 4/5, 2016 at 9:45 Comment(2)
adding java.rmi.server.hostname works for me. it can be either ip or hostnameDowne
Isn't java.rmi.server.hostname a name of the server you're connecting with VisualVM from?Nationality
O
0

All you need to do it set these VM options:

-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:FlightRecorderOptions=stackdepth=512

Oddball answered 15/4, 2017 at 19:52 Comment(1)
with mission controlOddball

© 2022 - 2024 — McMap. All rights reserved.