reference: https://www.geeksforgeeks.org/jvm-works-jvm-architecture/
https://www.geeksforgeeks.org/differences-jdk-jre-jvm/
Java Virtual Machine
- run Java applications
- calls the main method in java code
- a part of JRE java runtime environment
- When run the java class, an instance of JVM is created
When run java program, JVM is mainly for three activities: loading, linking, initialization
Loading
- class loader: reads the .class file, generate binary data and save it in method area
- JVM creates an object of class to represent this file in heap memory. (can be used to get class level information link name of class, parent name, methods and variable information)
Linking
Performs verification, preparation and optionally resolution
- verification: ensure the .class file is valid
- preparation: JVM allocates memory for class-level variables or static variables and assign default values to them
- initialization: replace symbolic name with memory reference in method area
Initialization
- assign value to all static/class-level variables and execute static block from parent to child class, from top to bottom
JVM memory
Method area:
all class level information, like class name, methods and variables information.
Only one method area/JVM, it is a shared resource
Heap area:
information of all objects.
Only one Heap area/JVM. It is a shared resource.
Stack area:
For every thread, JVM creates one run-time stack.
Every block in the stack store method calls and its corresponding local variables.
After a thread terminate, JVM will destroy the stack.
Not a shared resource.
PC registers:
store address of current execution instruction of a thread.
One PC register/Thread
Native method stacks:
One native method stack/thread
Execution engine
Read the byte-code line by line, use data and information present in various memory area and execute instructions. Three parts:
- Interpreter: read the byte code line by line and then execute.
- Just-In-Time (JIT) compiler:
- Garbage Collector: destroy un-referenced objects.
Java Native Interface
An interface between the Native Method Libraries and JVM. Enable JVM to call C/C++ libraries.
Native Method Libraries
A collection of the Native Libraries(C, C++) required by the Execution Engine.
Difference among JDK, JRE and JVM
JDK: provide environment to develop and run the Java program
JRE: provide environment to only run the Java program
JVM: run the java program line by line