12

Is there any Java profiler that allows profiling short-lived applications? The profilers I found so far seem to work with applications that keep running until user termination. However, I want to profile applications that work like command-line utilities, it runs and exits immediately. Tools like visualvm or NetBeans Profiler do not even recognize that the application was ran.

I am looking for something similar to Python's cProfile, in that the profiler result is returned when the application exits.

ejel
  • 4,135
  • 9
  • 32
  • 39
  • 1
    If it's short lived, why profile it? The whole impetus around profiling is when things take too long, right? – Will Hartung Apr 16 '10 at 23:13
  • 4
    In this case, short-lived that I referred to means something like command-line utilities which end momentary after executed. However, it is still possible that they are taking too long for the requirements (e.g. 30 sec where less than 5 sec is preferred). So it is still useful to pinpoint which parts are the bottlenecks. – ejel Apr 17 '10 at 06:17

10 Answers10

9

You can profile your application using the JVM builtin HPROF.

It provides two methods:

  1. sampling the active methods on the stack
  2. timing method execution times using injected bytecode (BCI, byte codee injection)

Sampling

This method reveals how often methods were found on top of the stack.

java -agentlib:hprof=cpu=samples,file=profile.txt ...

Timing

This method counts the actual invocations of a method. The instrumenting code has been injected by the JVM beforehand.

java -agentlib:hprof=cpu=times,file=profile.txt ...

Note: this method will slow down the execution time drastically.


For both methods, the default filename is java.hprof.txt if the file= option is not present.

Full help can be obtained using java -agentlib:hprof=help or can be found on Oracles documentation

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
VHF
  • 174
  • 3
  • I have tried hprof but it seems that it can only dump heap profile. Do you know if it is possible to also dump cpu time data to a file as well? – ejel Apr 20 '10 at 01:16
  • Completely revised the answer to current site standards (by adding useful information/examples) and fixed the broken link – try-catch-finally Aug 25 '17 at 21:03
  • hprof agent was removed in java 9, [JEP 240](http://openjdk.java.net/jeps/240) – user2418306 Sep 07 '20 at 18:23
5

Sun Java 6 has the java -Xprof switch that'll give you some profiling data.

-Xprof            output cpu profiling data
McDowell
  • 107,573
  • 31
  • 204
  • 267
4

A program running 30 seconds is not shortlived. What you want is a profiler which can start your program instead of you having to attach to a running system. I believe most profilers can do that, but you would most likely like one integrated in an IDE the best. Have a look at Netbeans.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
3

Profiling a short running Java applications has a couple of technical difficulties:

  • Profiling tools typically work by sampling the processor's SP or PC register periodically to see where the application is currently executing. If your application is short-lived, insufficient samples may be taken to get an accurate picture.

You can address this by modifying the application to run a number of times in a loop, as suggested by @Mike. You'll have problems if your app calls System.exit(), but the main problem is ...

  • The performance characteristics of a short-lived Java application are likely to be distorted by JVM warm-up effects. A lot of time will be spent in loading the classes required by your app. Then your code (and library code) will be interpreted for a bit, until the JIT compiler has figured out what needs to be compiled to native code. Finally, the JIT compiler will spend time doing its work.

I don't know if profilers attempt to compensate to for JVM warmup effects. But even if they do, these effects influence your applications real behavior, and there is not a great deal that the application developer can do to mitigate them.

Returning to my previous point ... if you run a short lived app in a loop you are actually doing something that modifies its normal execution pattern and removes the JVM warmup component. So when you optimize the method that takes (say) 50% of the execution time in the modified app, that is really 50% of the time excluding JVM warmup. If JVM warmup is using (say) 80% of the execution time when the app is executed normally, you are actually optimizing 50% of 20% ... and that is not worth the effort.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • +1 Good comments. I would only point out, regarding your first point about profiling tools, that 1) sampling the PC register is nearly useless (compared to sampling the stack) in all but the simplest programs, and 2) not many samples are needed, because accuracy of time measurement is not needed (assuming the goal is to find fruitful points to optimize). Here's more on that subject: http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343 – Mike Dunlavey Apr 17 '10 at 16:22
2

If it doesn't take long enough, just wrap a loop around it, an infinite loop if you like. That will have no effect on the inclusive time percentages spent either in functions or in lines of code. Then, given that it's taking plenty of time, I just rely on this technique. That tells which lines of code, whether they are function calls or not, are costing the highest percentage of time and would therefore gain the most if they could be avoided.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
2

start your application with profiling turned on, waiting for profiler to attach. Any profiler that conforms to Java profiling architecture should work. i've tried this with NetBeans's profiler.

basically, when your application starts, it waits for a profiler to be attached before execution. So, technically even line of code execution can be profiled.

with this approach, you can profile all kinds of things from threads, memory, cpu, method/class invocation times/duration...

http://profiler.netbeans.org/

kctang
  • 10,894
  • 8
  • 44
  • 63
1

The SD Java Profiler can capture statement block execution-count data no matter how short your run is. Relative execution counts will tell you where the time is spent.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
1

You can use a measurement (metering) recording: http://www.jinspired.com/site/case-study-scala-compiler-part-9 You can also inspect the resulting snapshots: http://www.jinspired.com/site/case-study-scala-compiler-part-10

Disclaimer: I am the architect of JXInsight/OpenCore.

William Louth
  • 207
  • 1
  • 4
0

I suggest you try yourkit. It can profile from the start and dump the results when the program finishes. You have to pay for it but you can get an eval license or use the EAP version without one. (Time limited)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

YourKit can take a snapshot of a profile session, which can be later analyzed in the YourKit GUI. I use this to feature to profile a command-line short-lived application I work on. See my answer to this question for details.

Community
  • 1
  • 1
Binil Thomas
  • 13,699
  • 10
  • 57
  • 70