Nov 15, 2017

Understanding JVM: Memory Areas in JVM - Part 3

We have already covered Method Area, Heap Area and Stack memory in previous articles. This article will give brief overview of the remaining 2 memory areas in JVM, i.e., PC Registers and Native Method Stack.

PC Register (Program Counter Registers)

  • It is internally used by JVM and like Stack Memory, every thread keeps a separate PC register, which is created at the time of thread creation.
  • It contains the address of currently executing instruction.
  • Once instruction execution completes, automatically PC register is incremented to hold the address of next instruction.

Native Method Stack

  • JVM creates a separate Native Method Stack per thread.
  • Again, it is internally used by JVM. All native calls invoked by thread will be stored in corresponding native method stack.

This concludes the topic of Memory Areas inside JVM. To summarize things:
  1. Method area, heap area and stack area are considered most important with respect to programmers.
  2. Method and heap area are per JVM.
  3. Stack memory, PC registers and Native Method stack are maintained per thread.
  4. Static variables are stored in Memory Area
  5. Instance variables are stored in Heap Area
  6. Local variables are stored in Stack Area

Let us write a simple program to understand things better: 

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package edu.javaterritory.example.methodarea;

public class Test {

 private Student s1 = new Student();
 private static Student s2 = new Student();
 
 public static void main(String[] args) {
  Test t1 = new Test();
  Student s3 = new Student();
 }
}

Following diagram shows where variables created in above program will be stored.

  • Static variables inside method area
  • Instance variables in heap area
  • Local variables in static memory area

So, that's all about method area's inside JVM. Hopefully it helps.
We will continue to remaining components inside JVM, i.e., Execution Engine and JNI (Java Native Interface).

Please provide your inputs in the comments section.
Also, you can follow us at +Java Territory (or here).

5 comments: