JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

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.

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 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

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
What Is Micro Benchmark?