-3

Hi I am trying to get the byte code count for some java codes using -XX:+CountBytecodes flag of Hotspot JVM. But this seems to give extremely dissimilar results when I perform parallel runs for the same code. Is this behavior expected? If yes, why? Is there any other profiler that would give constant byte code counts for the same code?

aichemzee
  • 91
  • 1
  • 6
  • 2
    Can you given an example of a method and the commands you used for counting the byte code? – Rafael Winterhalter Jul 28 '14 at 13:38
  • @raphw I used a debug build of the Hotspot JVM (which can be found [here](http://download.java.net/jdk6/6u25/promoted/b03/index.html)) and ran it with the -XX:+CountBytecodes flag. – aichemzee Jul 28 '14 at 14:00
  • Can you be more specific about 'Perform parallel runs for the same code' ? Are you running two JVM instances on a single machine (or) your using threads to run the code in a single VM ? You can always view your bytecode generated using javp command or anyother bytecode visualizer. – Arkantos Jul 30 '14 at 20:44
  • @Arkantos I am running two JVM instances on a single machine. And I want the execution count for the byte codes (that is how many byte codes were executed), not the actual byte codes. Are you implying that it is possible that on two instances, the byte codes generated were different? – aichemzee Jul 31 '14 at 05:43
  • For a given .java file, the byte codes generated by javac compiler are always same. But at runtime JVM makes certain optimizations that may exclude some of the byte codes while execution. I'd recommend you to run your code in 2 JVMs with same settings (same compiler) and execute it for same duration. If it's a simple main method that you're running, you won't be able to capture the results accurately. You have to do some warm-up before you actually measure anything. Keep your logic in a loop of say 1M iterations and then see your counts – Arkantos Jul 31 '14 at 18:24
  • A simple example for JVM optimizations is Dead Code Elimination. If you define a variable int i=0 and never use it in your method, even though the byte code is generated to load the integer constant 0, at runtime it will NOT be translated to native code because JVM realizes that i was never used. If you use a modern javac compiler that comes with JDK7/JDK8, these optimizations are done at compile time. Eclipse does that showing warnings :) – Arkantos Jul 31 '14 at 18:27

1 Answers1

0
  1. Bytecodes are counted only inside the interpreter, not in JIT-compiled code. Try -Xint
  2. The bytecode counter of HotSpot JVM is not thread-safe. It was never meant to be exact.
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Using -Xint didn't solve the problem. Still the counts are dissimilar for parallel runs of the same code. Is there any alternative profiler that could give better(almost constant) results across parallel runs? – aichemzee Jul 30 '14 at 05:58