Chapter 41. Java Programming from a JVM Performance Perspective
Monica Beckwith
Tip #1: Don’t Obsess Over Garbage
I find that sometimes Java developers obsess over the amount of garbage their applications produce. Very few cases warrant this sort of obsession. A garbage collector (GC) helps the Java Virtual Machine (JVM) in memory management. For OpenJDK HotSpot VM, the GC along with the dynamic just-in-time (JIT) tiered compiler (client (C1) + server class (C2)) and the interpreter make up its execution engine. There are a slew of optimizations that a dynamic compiler can perform on your behalf. For example, C2 can utilize dynamic branch prediction and have a probability (“always” or “never”) for code branches taken (or not). Similarly, C2 excels in optimizations related to constants, loops, copies, deoptimizations, and so on.
Trust the adaptive compiler, but when in doubt verify using “serviceability,” “observability,” logging, and all the other such tools that we have thanks to our rich ecosystem.
What matters to a GC is an object’s liveness/age, its “popularity,” the “live set size” for your application, the long-lived transients, allocation rate, marking overhead, your promotion rate (for the generational collector), and so forth.
Tip #2: Characterize and Validate Your Benchmarks
A peer of mine once brought in some observations of a benchmarking suite with various sub-benchmarks. ...