Profiling a running Java application in command line

KARASZI István picture KARASZI István · Jul 27, 2011 · Viewed 47.8k times · Source

I profile running Java applications often with VisualVM but it needs X to run on the machine.

I know I can connect through management port but that will be an offline sampled profiling which is not enough for me.

So I'm looking for a solution with which I can profile the CPU usage of the methods of a running Java application from command-line. It's enough for me to collect data on the server and then the collected data can be analyzed on a different machine.

Update:

It seems I need to be more specific. I want to profile a running Java application from command line, I don't want to stop it and rerun it.

Answer

Andrejs picture Andrejs · Jan 19, 2016

The jvmtop application is a convenient tool for profiling from the commandline. No need to stop the jvm. Usage:

jvmtop.sh --profile <PID>

Will give you output like this which will be updating while the app runs:

  Profiling PID 24015: org.apache.catalina.startup.Bootstrap
  36.16% (    57.57s) hudson.model.AbstractBuild.calcChangeSet()
  30.36% (    48.33s) hudson.scm.SubversionChangeLogParser.parse()
   7.14% (    11.37s) org.kohsuke.stapler.jelly.JellyClassTearOff.parseScript()
  ...

The advantage is that it does not take the use of instrumentation. The classes of the to-be-profiled jvm will not be altered.

If you are looking for something more visual then have a look at jvm-mon which is based on jvmtop