April 6, 2020
Estimated Post Reading Time ~

Notes on Java Hotspot JVM

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

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

  • Client Virtual Machine (VM) – Tuned for faster startup.
  • Server Virtual Machine (VM) – Tuned for maximum speed.
JDK VS 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



source: DZone

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

CLASS LOADING

Source: Oracle

Classloading in jvm is followed by 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 to the classes loaded by the children. This property allows OSGi kinds 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 a 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 a fast but stop-the-world event leading to pause. This implies that a higher frequency of GC run impacts the application performance. GC collectors generally follow a 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.

Source: http://www.computepatterns.com/1031/notes-on-java-hotspot-jvm/


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.