The Memory Area of JVM is responsible to store all information which are provided by Class Loader. It consists with 5 areas known as, Method Area, Heap, Stack, PC Register and Native Method Area. Let’s discuss about the responsibilities of each memory are in JVM.
- Method Area: Responsible to get class information from the compiled class.
- Head: Stores information about the objects of particular class.
- Stack: Stores information about the methods and local variables. For an example, when we invoke a method it will create a frame in stack. After execution of method, the created method will be destroyed.
- PC register: Holds information about the current execution.
- Native method: Holds information about the native methods itself.
Method area and Heap is created per JVM instance. But Stack, PC register and Native method area are created per thread.
Let’s simplify the mechanism happens in last 3 components (Stack, PC register and Native method) of memory area.
As shown in above figure, let’s assume we have 4 treads or JVM instances at the same time which are consists with different methods.
The first thread (T1) has 3 methods respectively M1, M2 and M3. T2 has 2 methods known as M1 and M2. Third thread has only M1 and T4 has 2 methods as M1, M2. But as you can see, T4 has a method named M2 which called a native method.
When we consider about the PC register, it holds the information about current execution. Therefore, it holds information about first 3 threads (T1, T2,T3) but not T4.
Because, T4 access the native method resides in native method area and PC register holds the information about Java methods not about the native method. That is why PC register does not hold any information about T4. In that case, information on T4 inside the PC register will be null or undefined.
Hope you get a clear understand about the memory area of JVM. Let’s discuss about the Execution engine in next article.
Stay Safe !!!