Memory Area in JVM
All the information provided by the ClassLoader will be stored in the Memory area in JVM. There are five main areas in Memory. They are,
· Method Area
· PC Register
· Native Method Area
When we talk about the Method area it is responsible to get the information of the class from the compilation.
When you load the class all the instances of the class come to the Heap area.
Stack is responsible to store information about methods and local variables. When we invoke a method in the class, it will create a frame in the stack.
PC register will hold the current execution information.
The native method holds information about the native methods themselves.
For one JVM instance, the Method area and the Heap area were created for one time. But this will change for Stack, PC register, and Native method area. They are created per thread.
Let’s demonstrate what happens in Stack, PC register, and Native method of the memory area.
As shown in the above image there are four threads that execute simultaneously. The first thread has three methods as M1, M2, and M3. The second thread has two methods as M1 and M2. The third thread has only M1. The fourth thread has two methods as M1 and M2. As shown in the above image Fourth thread has native method M2.
The PC register holds the information about current execution and it holds information about Thread one, two, and three only. Because thread four accessed the native method. PC registers only hold information about Java methods, not native methods. In this instance, thread four information inside the PC register will be undefined or null.