Profiling short-lived Java applications

Is there any Java profiler that allows me to profile short-term applications? The profilers that I have found so far seem to work with applications that continue to run until the user completes. However, I want to profile applications that work as command line utilities, they start and exit immediately. Tools such as visualvm or NetBeans Profiler do not even recognize that the application is running.

I am looking for something similar to Python cProfile, as the profiler result is returned when the application exits.

+7
java optimization profiling profiler
source share
10 answers

You can profile your application using the built-in HPROF JVM.

It provides two methods:

  • selection of active methods on the stack
  • runtime of the time method using injected bytecode (BCI, byte input of encoders)

Sampling

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

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

Timing

This method counts the actual method calls. The tool code was previously introduced by the JVM.

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

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


For both methods, the default file name is java.hprof.txt if the file= parameter file= absent.

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

+8
source share

Sun Java 6 has a java -Xprof switch that will give you some profiling data.

 -Xprof output cpu profiling data 
+5
source share

Profiling short Java applications has several technical difficulties:

  • Profiling tools typically work by periodically fetching the SP register or processor PC to find out where the application is running. If your application is short-lived, not enough samples can be taken to get an accurate picture.

You can solve this problem by changing the application to run several times in a loop, as suggested by @Mike. You will have problems if your application calls System.exit() , but the main problem is ...

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

I don't know if profilers are trying to compensate for the effects of JVM warming up. But even if it is, these effects affect the behavior of your applications, and not so much that the application developer can do to mitigate them.

Returning to my previous point ... if you run a short-lived application in a loop, you are actually doing something that modifies its normal execution pattern and removes the JVM warm-up component. Therefore, when you optimize a method that takes (say) 50% of the execution time in a modified application, it really is 50% of the time, excluding the JVM warm-up. If the JVM warm-up uses (say) 80% of the runtime when the application runs normally, you are actually optimizing 50% to 20% ... and it's not worth the effort.

+4
source share

A program that runs for 30 seconds is not short-lived. What you want is a profiler that can run your program instead of connecting to a running system. I believe that most profilers can do this, but most likely you will like the one integrated into the IDE. Take a look at Netbeans.

+4
source share

If this does not take a lot of time, simply circle a loop around it, an infinite loop if you want. This will not affect the included percentages of time spent on either functions or lines of code. Then, given that it takes a long time, I simply rely on this technique . This indicates which lines of code, regardless of whether they are function calls or not, are worth the highest percentage of time and therefore will get the most benefit if they could be avoided.

+2
source share

run the application with profiling enabled, waiting for the profiler to join. Any profiler that conforms to the Java profiling architecture should work. I tried this with the NetBeans profiler.

basically, when your application starts, it expects the profiler to be attached before execution. Thus, technically even a line of code execution can be profiled.

using this approach you can profile all kinds of things from threads, memory, processor, time / duration of a method / class call ...

http://profiler.netbeans.org/

+2
source share

SD Java Profiler can display the execution data of a statement execution block no matter how short your run is. A relative execution score will show you where the time is spent.

+1
source share

You can use the measurement record (measurements): http://www.jinspired.com/site/case-study-scala-compiler-part-9 You can also check the images taken: http://www.jinspired.com/site / case-study-scala-compiler-part-10

Disclaimer: I am the architect of JXInsight / OpenCore.

+1
source share

I suggest you try yourkit. He can form a profile from the very beginning and discard the results when the program ends. You have to pay for it, but you can get an eval license or use the EAP version without it. (Limited time)

0
source share

YourKit can take a snapshot of the profile session, which can then be analyzed in the YourKit GUI. I use this function to profile an application with a short command line duration that I'm working on. See my answer to this question for more details.

0
source share

All Articles