Notes on Java HotSpot JVM

Summary of my brush-up on popular JVM implementation – HotSpot from Oracle.

HotSpot comes in 2 flavours. Both the flavours use the same code but uses different compilers altering the optimization schemes such as method inlining and heap policies. Flavour could be chosen by setting command line parameters -server or -client. By default, the launcher specific to the platform chooses the flavour if the value is not supplied as command line option.

  • Client Virtual Machine (VM) – Tuned for faster startup.
  • Server Virtual Machine (VM) – Tuned for maximum speed.

JDK Vs JRE

jdk-jre
source: Oracle.com

JRE (Java Runtime Environment) provides the virtual machine (JVM), base libraries and other components required to run java applications. JDK (Java Development Kit) includes JRE and tools necessary for development such as compilers, debuggers, etc.

Memory layout

Memory space Purpose
Java heap Primary memory space for java class instances.
Metaspace Class metadata. Part of native memory. From java 8, permgen (Permanent generation) has been replaced by metaspace
Native heap Code cache, Memory mapped files, Memory for native libraries, threads, stack, etc.

Code Compilation

jvm-code-compilation
source: DZone

Java code (.java) are compiled into bytecode (.class) files. Bytecode are input to the JVM. JVM interprets the bytecode and produces native code to be executed by operating system in the hardware. Typically the process of interpretation is slow and impacts the performance. Hence, HotSpot uses adaptive compilation process using Just-In-time Compiler (JIT). Code is interpreted and analysed to detect the frequently executed code (hot spots). Such hot spot code are compiled and cached avoiding re-compilation to improve the performance. JVM also applies optimization such as method inlining.

Class loading

jvm-class-loading
Source: Oracle

Class loading in jvm is follows a delegation hierarchy. A class loader attempts to find the class in the parent hierarchy before loading the class. In effect, a class gets loaded only if it’s not available with the parents. Classes loaded by the parent cannot refer the classes loaded by the children. This property allows OSGi kind of systems to have class loaders per bundle allowing multiple versions of the same code to co-exist.

Garbage Collection

Heap space in JVM has Young Generation (Young gen) and Old Generation. Young Generation is of short-lived objects while the Old Gen is for long-lived objects. Objects from Young Gen gets promoted to Old Gen. Garbage Collection is Young Gen is fast but stop-the-world event leading to pause. This implies that higher frequency of GC run impacts the application performance. GC collectors generally follow mark and sweep approach. Modern JVMs support serial (Young Gen first and the Old Gen next in sequence), parallel (Young Gen and Old Gen in parallel threads) and concurrent (GC run concurrently with application execution) collectors. Care must be exercised while choosing the collector.

References

Notes on Java HotSpot JVM