Wednesday, January 5, 2022

JVM Architecture - Part -2

JVM Architecture - Part -2


This is continuation to part - 1 of the JVM architecture - 


1.    Runtime Data Areas

Runtime Data areas is the actual memory area of your java application.

There are 5 different Run time Data areas.

a. Method Area

b. Heap

c. PC Registers

d. Java Stacks

e. Native Method stacks

 





a.     Method Area

the method area consists of class level data

class level static variables

class level constant pool

the byte code

The class meta data that is required for java reflection api.

The method area is space allocated to you JVM

The method ares is also called perm gen space

By default 64MB memory is allocated to method area

This can be tuned or updated by using –XX:MaxPermSize

Suppose if you are loading so many classes and all of those classes meta data or data cannot be stored in Method Area, then you application will get java.lang.OutOfMemoryError for Permgen space

from Java 8 onwards, permgen or Method Area is removed and is moved to some memory location called metaspace in native operating system memory. So from java8 onwards, there is no limit for the method area, it can use whatever memory is available in the native operating system.

b.     Heap

Whenever any objects are created in your classes, those objects are created inside Heap.

All instance variables of a class are created in Heap.

Arrays are also objects, so they are also created in heap.

we can tune or update the minimum heap memory using –Xms and maximum heap memory using –Xmx.

c.      PC Registers

Program Counter Registers which is a pointer to the next instruction to be executed per thread.

Suppose if we are running 3 threads, 3 PC registers will be created each PC register will be pointing to the next instruction to be executed of that specific thread for which it is created.

 

d.     Java Stacks

·       Java stack memory areas are created per thread.

·       Each stack frame contains the methods that is currently in execution.

·       Method local variables and all computations or operations done inside a method are done in java stacks.      

Suppose if we have 3 threads T1, T2 and T3

T1 is executing 3 methods -> 3 stack frames are created

T2 is executing 2 methods -> 2 stack frames are created

T3 is executing 1 method -> 1 stack frame is created

Suppose the thread T1 is calling method1 then a stack frame will be created for this thread. If method1 is calling method2 one more stack frame is added on top of method1’s stack frame. Once the method2 execution is completed, it will be popped from the stack and method1 completes the execution.

We have an example of recursion where a method calls itself in the execution.

Suppose if we forgot to write exit condition for recursion, the stack frames will keep on piling up and it will result in java.lang.StackOverflowError. In such cases you have to find out what are the reasons why the stack frames are not getting popped up.

-Xss is used to tune or update the stack memory area.

e.     Native Method Stacks

If java method calling any native method for loading native libraries like dll files for execution, those native methods will be executed in native method stacks.

We talked about

Method area is for Class data

Heap is for object data

PC registers is for storing the address of next instruction of a thread

Java stack is load and execute the method of a thread

Native stacks is to load and execute the native methods of a thread

PC Registers, Java stacks, Native Stacks are per thread, which means that stack frame of thread T1 cannot access the stack frame of thread T2 and other threads. So whatever happens in the method execution is a kind of thread-safe

Method Area, Heap are not per thread but they are per JVM.


Execution Engine concept is explained here.

No comments:

Post a Comment

Difference between Procedure Oriented Programming System and Object Oriented Programming System (POPS vs OOPS)

POPS vs OOPS   Today we will get to know the difference between Procedure oriented Programming system (POPS) and  Object-Oriented  Programmi...