JVM architecture and Memory Management
What is JVM?
JVM is an abstract machine used to run(execute) Java Program.There are three notions of JVM: specification, implementation, and instance.
- Specification : A document that describes what is required of JVM Implementation.
- Implementation: Known as JRE(Java Run Time Environment.)
- Instance: Whenever you will run a java class file an instance of JVM is created.
Note: Each byte code is executed line by line by the JVM, that’s why JVM is called as an interpreter.
JVM Internal Architecture and Memory Management
As shown in picture, we can divide JVM into three parts:
- Class Loader Subsystem
- Runtime Data Area
- Execution Engine
Class Loader Subsystem
- It is used to load the classes and the interfaces.
- It verifies the byte-code instructions.
- It is used to assign values to class variable or static variables.
Runtime Data Area
It is the memory resource used by the JVM and it is of Five types:-
1) Method Area:
- The method area is used to store the class data and method data including static methods.
- There is only one method area per JVM and it is a shared resource.
2) Heap Area:
- The Heap Area is used to store the objects and their corresponding instance variables and arrays.
- There is only one Heap area per JVM, hence Method and Heap area are shared among resources so the data stored is not thread safe.
3) Stack Area:
- For every new thread a new stack will be created,
- All Local variables will be created and stored in stack area.
- Since it is not a shared resource, so it is thread safe.
4) PC Registers
- PC(Program Counter) Register contains the address of the JVM instruction, currently being executed.
5) Native Method Stack
- It contains all the Native method used in the application.
The byte-code which is assigned to Runtime data area will be executed here.
- It interprets the bytecode faster and but executes code slowly.
- The disadvantage of the interpreter is that when one method is called multiple times, every time a new interpretation is required.
2) JIT Compiler:
- It is used to improve the performance.
- The Interpreter is used in converting the bytecode, but when it finds the repeated code it will use the JIT compiler.
- The JIT Compiler compiles the entire bytecode and changes it to native code.
- This native code will be used directly for repeated method calls, which improve the performance of the system.
3) Garbage Collector:
- Collects and removes unreferenced objects.
- Garbage Collection can be triggered by calling “System.gc()”
Java Native Interface (JNI):
- JNI is used to interact with the Native Method Libraries and provides the Native Libraries required for the Execution Engine.
Native Method Libraries
- It is a collection of the Native Libraries which is required for the Execution Engine.
Hope you find easy to understand the concepts. Please like, share and subscribe for similar articles on Java.