JVM Architecture Introduction | JVM Internals
Being a java developer, it is expected from us to have basic knowledge of JVM architecture, as it enables us to write code more efficiently. Also from the interview perspective JVM architecture, Class loader subsystem, JVM memory management are frequently discussed topics with 3-5 year experienced java developer. If you are able to answer these questions, you will create good impression on interviewer. In this article we will see basic Architecture of JVM and its Components.
What is Virtual Machine ?
"virtual" means it is an abstract computer defined by a some specification. This VM acts as runtime engines to run particular programming language applications.
e.g. JVM (Java virtual machine) acts as runtime engine to run java based application. PVM (Parallel Virtual Machine) acts as runtime engine to run Perl based application. CLR ( Common Language Runtime) acts as runtime engine to run .net based applications.
What is a Java Virtual Machine?
The Java Virtual Machine is an abstract computer defined by a Java related specification.
After you create a Java program, you have to compile this program. The result of this compilation is the file with extension .class. It contains Java bytecode. The generated bytecode is identical no matter on what platform you have compiled your program. It is platform neutral. That is why we can say that Java program is platform independent.
However, you cannot execute this bytecode yet because your system understands machine instructions that are unique to your system’s architecture (PowerPC, Intel, and so on). In order to execute a Java program, you have to install a middleware component that fits between your Java program and operating system. This component is called Java Virtual Machine.
JVM runs as an application on top of an operating system. JVM effectively reproduce Operating system environment for Java programs. Thats why it is called Java Virtual Machine.
The main purpose of JVM is to convert the Java bytecode to the machine instructions that you can execute on the hardware platform where you run your application.Converting Java bytecode is not the only task that JVM performs.
The Architecture of the Java Virtual Machine
When a Java Virtual Machine runs a program, it needs memory to store many things, including bytecodes and other information it extracts from loaded class files, objects the program instantiates, parameters to methods, return values, local variables, and intermediate results of computations. The Java Virtual Machine organizes the memory it needs to execute a program into several runtime data areas.
Basic Architecture Diagram of JVM
In Java, JVM is part of JRE and it is responsible to load and run JAVA class files. Basically JVM is divided in 3 parts as follows
1. Class Loader SubSystem
Java’s dynamic class loading functionality is handled by the class loader subsystem. It loads, links and initializes the class when it refers to a class for the first time. Class loader sub system is responsible for following 3 activities
- Loading :The Class loader reads the .class file, generate the corresponding binary data and save it in method area.
- Linking : Performs verification, preparation, and (optionally) resolution.
- Initialization : In this phase, all static variables are assigned with their values defined in the code and static block(if any). This is executed from top to bottom in a class and from parent to child in class hierarchy.
To know more about Class Loader SubSystem, refer our article Class Loader SubSystem | JVM Internals.
2. Memory Management Area
Java runtime Memory Area is divided into 5 parts.
- Method area : In method area, all class level information like class name, immediate parent class name, methods and variables information etc. are stored, including static variables. There is only one method area per JVM, and it is a shared resource.
- Heap area : Information of all objects is stored in heap area. There is also one Heap Area per JVM. It is also a shared resource. Since the Method and Heap areas share memory for multiple threads, the data stored is not thread safe.
- Stack area : For every thread, JVM create one run-time stack which is stored here. Every block of this stack is called activation record/stack frame which store methods calls. All local variables of that method are stored in their corresponding frame.The stack area is thread safe since it is not a shared resource. After a thread terminate, it’s run-time stack will be destroyed by JVM.
- PC Registers : Each thread will have separate PC Registers, to hold the address of current executing instruction once the instruction is executed the PC register will be updated with the next instruction.
- Native method stacks : For every thread, separate native stack is created. It stores native method information.
3. Execution Engine
The Execution Engine of JVM is Responsible for executing the program and it contains two parts.
- JIT Compiler (just in time compiler).
The java code will be executed by both interpreter and JIT compiler simultaneously which will reduce the execution time and them by providing high performance.
Java Native Interface (JNI)
It is a interface which interacts with the Native Method Libraries and provides the native libraries(C, C++) required for the execution.
Native Method Libraries
It is a collection of the Native Libraries(C, C++) which are required by the Execution Engine.
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.
About the Author
Tags: Class Loader, Core Java, Heap, Java, Java Memory Model, JVM internals
Comments and QueriesIf you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.