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.
Stephen c
source share