Decoding The JVM Architecture — Here’s How JVM Works
Java Virtual Machine is the core of every Java ecosystem that allows Java programs to follow the “code once, run anywhere” approach. As a developer, you can write a Java program on one machine and it will run flawlessly on every machine using the JVM, that too, with complete platform-independency and object-oriented programming.
The Concept of a Virtual Machine
But before we understand what a Java Virtual Machine is, let us first recap the concept of a Virtual Machine or VM. Being a virtual representation of a physical computer, a VM can also be termed as a guest machine running its own operating system and applications, irrespective of what platform or application the physical machine is running on.
What is the Java Virtual Machine?
Fun Fact — The Java Virtual Machine was originally designed to run on set-top boxes. But when Sun Microsystems realized that the industry still wasn’t ready in the mid ’90s, they decided to implement JVM into desktop computers.
Do you know why languages such as C and C++ are called compiled programming languages? Because they are first compiled into platform-specific machine code. However, for JavaScript and Python, the computer directly executes the instructions without having to compile them first. These languages are therefore called interpreted languages.
Java, on the other hand, benefits from a combination of both. In Java, programs are first compiled into byte code to generate a class file, which is then interpreted by the Java Virtual Machine to get executed for underlying platforms. This makes Java entirely platform independent, an approach in which, the class file can be executed on any version of JVM running on any platform, hardware, or operating system. Similar to Virtual Machines, as explained above, JVM creates an isolated virtual environment on a host machine to execute Java programs directly without compiling.
Understanding The Architecture Of JVM
Image source: FreeCodeCamp
As shown in the diagram above, the architecture of JVM consists three fundamental components; The Class Loader, Runtime Memory/Data Area, and Execution Engine. Let’s now have an in-depth understanding of each component.
- The Class Loader
When a .java source file is compiled, it’s first converted into byte code as a .class file. Upon executing the program, the class loader then loads the program into the main memory. The first class loaded into the memory of the JVM is usually the class the contains the main() method. Within the Class Loader, there are three fundamental phases in the loading process; Loading, Linking, and Initialization. To learn more on how the Class Loader in JRE dynamically loads Java classes into the Java Virtual Machine, you can read through this in-depth article from GeeksForGeeks. - Runtime Memory/Data Area
In the architecture of JVM, there are five fundamental types of memory/data areas that dictates how JVM works: - Class (Method) Area — stores the class, variable, static, and method code such as the runtime constant pool.
- Heap — is the memory block where objects are created and stored to allocate memory to objects at runtime.
- Stack Memory — is used to store partial results that might be needed for performing dynamic linking.
- Program Counter Register — stores the return address of the JVM or a native pointer on a specific platform.
- Native Method Stack — is allocated for each thread when it’s created and can either be fixed or dynamic.
- Execution Engine
Once the bytecode is finally loaded into the primary memory, and all necessary data is available in the runtime data area, the next step is to execute the program. In this step, the Execution Engine runs the code present in each class once it’s compiled. However, before executing the program, the bytecode must be converted into machine language instructions. This is where the JVM uses its JIT compiler — an interpreter for the Execution Engine that compiles the entire bytecode and translates it into native machine code, which further improves the performance of the system.
Conclusion
In this article, we discussed the components in the architecture of JVM and how JVM works. Still, there are internal mechanics of the JVM that requires an extensive elaboration. As a suggestion to our next article, be sure to drop your comments on what concept you want to learn next and we will try to cover it for you. Till then, happy coding. Also if you want to learn Java to become professional developer then join Geekster’s full stack web development course with java that includes real world projects for practice.