Java applications run on a Java Virtual Machine (JVM) so understanding how Java applications work in the JVM will help us understand how to organize and write code more efficiently. Currently, we have a variety of JVMs built by different companies based on the JVM specification: HotSpot JVM (Oracle), JRockit (Oracle), Jamiga (for the AmigaOS platform), … but the most popular is Oracle’s HotSpot JVM. In this tutorial, I will talk to you about the components of this virtual machine!
The main components of HotSpot JVM include VM Runtime, JIT Compiler, and Garbage Collector.
VM Runtime is the main component that provides common services and APIs for JIT Compiler and Garbage Collector. It also provides the basic functions of HotSpot JVM as a virtual machine startup, thread manager, Java Native Interface, …
The main functions of VM Runtime include:
Analyzes the arguments that we pass when we start the virtual machine
When booting VMs, we will definitely have multiple options attached and VM Runtime will be tasked with analyzing these options and configuring the HotSpot JVM based on these options.
This configuration will surely affect the performance of the HotSpot JVM. There are three categories for the command line option, which are:
- Standard Options: These are the options that all JVMs must have, defined by the JVM specification.
- Nonstandard Options: These options start with the “-X” character. Of course, these options are not guaranteed to be available in all JVMs.
- Developer Options: These options start with “-XX”. These options are specific to each system.
You can see more information about the HotSpot JVM option here.
Virtual machine lifecycle management from startup to shutdown.
Here we have many tools to start the virtual machine such as: run the program using java command, run Java Web application using javaws command, ..
VM Class Loading.
VM Runtime is responsible for loading objects into memory for execution in various ways such as: Class.forName (), ClassLoader.loadClass (), …
To ensure that, after the compile .class file is compiled by a trusted javac command, Java will have the mechanism to re-verify the bytecode. This verification will rely on a lot of constraint in bytecode!
Based on a table called TemplateTable that contains machine code corresponding to each bytecode for each operating system, the VM Runtime interprets each bytecode into machine code so that our program can execute on that operating system.
Any exception occurs, VM Runtime will be responsible for notifying the exception to the user.
Translating each bytecode into machine code will take a lot of time. The same code, if you have to repeat the translation with them, will not be effective. To improve this, the JIT Compiler concept has been introduced.
This is the component used to interpret the same bytecode in our program to the machine code. VM Runtime will analyze the code that is executed several times to transfer to JIT Compiler to translate into machine code.
In a Java application, Java objects will be created a lot. Some of them are used throughout the life cycle of the application from running until it ends. And there will be Java objects created just for a moment and will not be used anymore. If left unused objects exist in memory, then surely memory will soon be full and the program cannot run anymore.
To solve this problem, unused objects must be freed from memory. For languages like C C ++, the freeing of memory would be done manually using the code. But for Java, this will happen automatically through a process called Garbage Collection.
The Garbage Collection process is performed by the Garbage Collector.