JVM (1)-JVM Memory Area
Overview
Java is the well-known language that has the slogan “write once, run everywhere”, and the exist of JVM is exactly the reason why this slogan can be achieved. Specifically, when we write and run the Java code, the .java file is ultimately translated into bytecode, and then be loaded in the JVM to be implemented. This means that as long as we have a JVM in our machine, we can run the compiled JAVA file whatever the OS we are using.
JVM divides the memory into different parts to better manage it, we can say the the process that JVM load and create a Class/Instance is exactly the process that it put data into different parts of memories.
There are FIVE areas in JVM memory, with 2 of them are shared by threads (Heap, Method Area) and 3 of them (Stack, Native Method Stack, Program Counter) are thread-private.
Thread Sharing Areas
Heap
Heap is created as soon as the JVM is started.
It occupies the biggest memory, and also it is the main area that GC focuses on.
The only function of this area is “Storing the instances”, which means everything that was created by “new” key word in Java will be stored in this area, including array since array is also created by new.
Note that lots of articles say Heap itself is further divided into different areas: Eden Space, Survivor Space, Old Gen, this is not fully correct. All these terms come from another angle – Garbage Collection. From memory model aspect, there is only heap.
Method Area
In one word, Method Area is used to store all class-related information such as fields, constant, static variables, etc.
One thing to be noticed is the Runtime Constant Pool. Generally speaking, the so called runtime constant pool is part of space inside Method Area that used to store Constant Pool of the class file.
To be more specific, when java compiles .java file into bytecode, there are strict rules about the bytecode structure. It contains different units and the order between them are constant. Among these units, one thing is the constant pool.
Constant pool records the information about constant and symbolic references of the class file. One thing to be notice is that “Constant Pool” is part of the .class file. Runtime Constant Pool is part of the Memory where the constant Pool is stored.
Thread Private Areas
Program Counter
Similar to the CS register, indicating the line number of current instructions that is being implemented.
It is thread private because when current thread is interrupt and out of CPU, PC can help CPU knows which instruction to be run when the threads come back.
Stack
Lifecycle is same as thread.
Every thread has a stack, and every time a method is called, a stack frame will be created in the stack.
The process that a method is implemented is the process that the stack frame being pushed and pop out to/from the stack.
Stack Frame records following information:
Local Variable Table:
function parameters, local variables.
Operand Stack:
When we do calculation operations inside a method, it is actually done by Operand Stack.
It is also used to pass the parameters when calling another method inside a method.
Dynamic Linking
When a method A calling method B, JVM needs to know where B is stored.
This information is stored in the Constant Pool.
Native Method Stack
Same as Stack, but used by native methods.
Created by Shain at 2021/12/1, Melbourne, Australia.