JVM Tutorials - Herong's Tutorial Examples
∟Micro Benchmark Runner and JVM Options
∟What Is Micro Benchmark?
This section describes what is Java micro benchmark and general guidelines on writing micro benchmark programs.
What Is Micro Benchmark?
Micro benchmark is a benchmark designed to measure the performance of a very small and specific piece of code.
One good example of Java Micro Benchmark programs is the Java Microbenchmark Applet maintained by
Jonathan Hardwick at
http://www.cs.cmu.edu/~jch/java/microbench.html. It is designed to measure
the performance of Java basic operations like:
- Loop overhead: while (Go) n++
- Local variable assignment: i = n
- Instance var. assign.: this.i = n
- Array element assign.: a[0] = n
- Byte increment: b++
- Short increment: s++
- Int increment: i++
- Long increment: l++
- Float increment: f++
- Double increment: d++
- Object creation: new Object()
- Array creation: new int[10]
- Method call: null_func()
- Synchronous call: sync_func()
- Math function: abs()
- Inline code: (x < 0) ? -x : x
If you want to write your own micro benchmark programs, you need to consider:
- Compiler optimization - Compiler may modify your code for optimization. For example,
expression "Hello " + "Herong!" will probably be optimized as "Hello Herong!".
You need to write your code carefully to avoid compiler optimization.
- Java Virtual Machine (JVM) warm up - When JVM is started, it runs your application
thread together with other threads to warm up the system. If you run your benchmark test
immediately as after JVM started, warm up threads may affect your test code performance.
You need to give JVM a warm up period before starting your benchmark test code.
- Garbage Collection (GC) - GC thread may affect your test code performance.
You need to avoid garbage collection or turn off garbage collection if possible.
- Just In Time (JIT) compilation - Some JVM like HotSpot identifies code hot spots
and compile them into native code to speedup execution. If you want to test Java class
code interpretation speed, you should turn off JIT compilation. If you want to test native
code speed, you should JIT compilation of your test code first.
- Class loading and initialization - Be aware class loading and initialization effects
on your test code. You need to load all classes before starting your benchmark test code.
- Class loading and initialization - Be aware class loading and initialization effects
on your test code. You need to load all classes before starting your benchmark test code.
- Environment noise - Applications other than the JVM running on your computer may affect
your test code performance. You need to turn off other applications, like virus scanner,
Yahoo messenger, etc., as much as possible on your computer before running your benchmark test code.
Table of Contents
About This Book
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
JVM Runtime Data Areas
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
CDS (Class Data Sharing)
►Micro Benchmark Runner and JVM Options
►What Is Micro Benchmark?
BenchmarkRunner.java - Benchmark Runner Program
emptyLoop() - The Empty Loop Test Method
"-XX:+PrintCompilation" - Watching JIT Compilation Logs
"-XX:+PrintGC" - Watching GC (Garbage Collection) Logs
"-Xms" and "-Xmx" - Avoiding GC with Large Memory Size
Benchmark Affected by Other Running Applications
"-Xint" - Running in Interpreted-Only Mode
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode
Micro Benchmark Tests on "float" and "double" Operations
OpenJ9 by Eclipse Foundation
JRockit JVM 28.2.7 by Oracle Corporation
Archived Tutorials
References
Full Version in PDF/EPUB