Full explanation of JVM – Runtime Data Area and How JVM using it

Reading Time: 3 minutes

Runtime Data Areas are responsible to provide memory to store bytecode, objects, parameters, local variables, return values and intermediate results of computations. It’s a part of JVM. The JVM organises the memory it needs to execute program into serval runtime data areas.

JVM totally contains five runtime areas

  1. Method Area
  2. Heap Area
  3. Java Stacks Area
  4. Program Counter Registers Area
  5. Native Methods Stacks area

Method Area

  • In method area every class bytecode is load and store.
  • It means the given class static and instance variables declaration statements, blocks, methods, and constructors logic will be store in method area.
  • Method area create on start-up of virtual machine.
  • Method area is sharable memory area to all the threads.

How a class byte code is exactly stores in JVM’s method area?

  • We know ClassLoader stores the given class bytecode in JVM by using java.lang.Class object.
  • The java.lang.Class object internally uses serval other classes objects for storing given class bytecode.
  • The classes which are use for storing class bytecode in JVM are collectively known as Reflection API.
  • Below classes are classes Reflection API classes
    • Class
    • Package
    • Field
    • Constructor
    • Method
    • Modifier

Heap Area

  • Heap area is the main memory of JVM.
  • All classes and arrays objects are create in heap area.
  • Means given class instance variables memory allocate in heap area.
  • For storing given class bytecode inside JVM, once separate instance created from java.lang.Class. As many classes are loading into JVM those many instances are create for java.lang.Class in heap memory.
  • If there is no sufficient memory in method and heap area for loading new class bytecode or for creating new object, then JVM will throw exception java.lang.OutOfMemoryError.
  • The heap memory size can be increase at the time of setting up of runtime environment using non standard option as show in the below command
    • java -xms <size> classname

Java Stack Area

  • In this runtime area, all methods(non-native), blocks and constructors logic is executes.
  • In this runtime area we can create serval threads for executing methods at a time concurrently.
  • JVM by default creates minimum two threads, they are
    1. Main thread
      • Main thread is responsible to execute java methods starts with main method, also responsible to create objects in heap area if it finds “new” keyword in any method logic.
    2. Garbage collector thread
      • Garbage collector thread is responsible to destroy all unused objects from heap area.
      • Like in C++, in java we don’t have destructors to destroy objects.
  • As each new thread comes into existence, it gets its own pc register and java stack, the java stack is composed of stack frames.
  • A stack frame contains the state of one java method invocation.
  • When we invoke a method or constructor a new stack frame is create. Once this method execution complete this stack frame is destroyed.
  • Stack frame holds logic of this method, it’s parameters, local variables, its return value, and intermediate calculations. Logic of this method or constructor is loaded in this from method area.
  • Inside a thread if there is no sufficient memory for creating new tack frame for execution a method, the JVM will throw exception java.lang.StackOverflowError.

Program Counter Registers Area

  • In this runtime area, a separate program counter register is create for every thread.
  • It is used for tracking execution in this thread by storing its instruction address.
  • Once the execution is complete, it’s destroy automatically.

Native Methods Stack Area

  • In Native method stack area, all native methods are executes.
  • In java, there are two kinds of methods:
    1. Java methods
      1. A java method is written in the java language, compile to byte codes, and store in class files.
    2. Native methods
      1. A native method is written in some other language, such as C, C++, or assembly, and compile to the native machine code of a particular processor.
  • To create native method, we must place native keyword in its prototype and it should not have body. A java program interacts with the host OS by invoking native methods.

Class Example{

public static native int add (int x, int y);  //it is a native method.

public static void main(String[] args){

add(10, 20)

}

}

  • The above program add(int, int) method is a native method. It is compile fine, but in execution JVM will throw exception: java.lang.UnsatisfiedLinkError.
  • This is because we just defined native method, but we did not defined it required C program and not linked.

Reference Links

To understand full JVM architecture : https://blog.knoldus.com/how-the-new-jvm-works-and-its-architecture/


Knoldus-blog-footer-image

Leave a Reply