How do you use HotSpot optimizations (inlining, EA) with a simple code example?

The HotSpot JVM comes with several optimizations that improve performance, such as method inlining and escape analysis (EA). These optimizations are performed at runtime as the JVM analyzes the code being executed. Using these features effectively can lead to significant performance improvements, especially in performance-critical applications. Here's a simple example to illustrate how HotSpot optimizations work:

class SimpleExample { public static void main(String[] args) { long sum = 0; for (int i = 0; i < 1_000_000; i++) { sum += computeValue(i); } System.out.println("Total sum: " + sum); } // This method can be inlined for better performance during tight loops static int computeValue(int i) { return i * 2; // simple calculation } }

HotSpot JVM inlining escape analysis performance optimization Java.