Java JIT loop unrolling policy?

If the JVM unrolls the loop is probably best answered by actually printing the generated assembly. Note that this requires your code to actually be executed as a hot spot (i.e. the JVM considers it worthy of the expensive optimizations).

Why the JVM decides one way or another is a much harder question and probably requires in-depth analysis of the JIT code.


Another way to see if loop unrolling is being performed in the loop is to specify -XX:LoopUnrollLimit=1 as an argument to the JVM when running your code.

If you have an executable jar, then an example of how you can use this is:

java -XX:LoopUnrollLimit=1 -jar your-jar.jar

This flag will

Unroll loop bodies with server compiler intermediate representation node count less than this value

And that'll directly address your question without needing to look at the generated assembly