Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling a running Java application in command line

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.

like image 600
KARASZI István Avatar asked Jul 27 '11 14:07

KARASZI István


People also ask

How do I profile my Java application?

Choose Profile > Advanced Commands > Run Profiler Calibration from the main menu. Choose the Java Platform to be used for profiling and click OK.

How do Java profilers work?

Sampling profilers work by periodically querying the JVM for all the running threads and getting the stack trace for each thread. It then determines what method each thread was executing when the sample was taken and compares the samples to determine how much time was spent in that method.

Is JVM a Java profiler?

A Java Profiler is a tool that monitors Java bytecode constructs and operations at the JVM level. These code constructs and operations include object creation, iterative executions (including recursive calls), method executions, thread executions, and garbage collections.


2 Answers

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

like image 113
Andrejs Avatar answered Oct 14 '22 06:10

Andrejs


Looks like the "built-in" way to profile a java app from the command line is to start it with profiling command line parameters, like this

$ java -Xrunhprof:cpu=samples,file=myprogram.hprof ... 

Then examine the file "myprogram.hprof" with some GUI tool (or web server tool like jhat) or command line tool after the process exits (and the file is created at that time).

If you use the "QUIT" signal trick, mentioned https://stackoverflow.com/a/2344436/32453 then you can generate a file at will without exiting the JVM (it appears to append to the previous output file). Or wait until the process exits and it will generate the file.

This (built-in) profiler does a sample infrequently so typically low slowdown/impact overall.

ref: http://web.archive.org/web/20160623224137/https://thunderguy.com/semicolon/2004/04/18/profiling-a-java-program-easily/

You could also just do the "poor man's profiler" by collecting lots of jstacks and dumping them into ex: a flamegraph or some other analyzer/conglomerator...

like image 36
rogerdpack Avatar answered Oct 14 '22 08:10

rogerdpack