Runtime Memory Area

Runtime Data Areas

When JVM starts, it creates runtime memory areas:

These are divided into two categories:

Thread Shared (Not Thread Safe)

  • Heap 
  • Method Area (Metaspace)

Thread Private (Thread Safe)

  • Stack
  • PC Register
  • Native Method Stack


Heap (Shared)

When we creates an object then it stores in Heap either it creates with new keyword or clone or reflection etc. 

But important thing is it all stores in Heap seperately.

  • Heap Memory is shared by all Threads
  • It is managed by Garbage Collector
  • It is the Largest Memory Area


Heap Memory divides into two part:

  • Young Generation
    • Eden
    • S0
    • S1
  • Old Generation

If Heap Fails: java.lang.OutOfMemoryError 

Object stored in Young Generation, when if it is retain for long period then it moves to Old Generation.


Method Area (Metaspace)

In this memory area JVM stores Class Metadata, Method Metadata, Variable Information, Constructor, Runtime Constant Pool, Static Variable etc 

After Java 8+

Method Area implemented as Metaspace


Difference between Method Area and Metaspace

Method Area is a logical space which contains the Class level information but this logical space has no fixed place whether is exists in Heap Memory or where no fixed size only JVM was aware that there is a place which consist the Class level information which was stored during class loading.

In java 6 and 7 PermGen was the implementation of the Method Area, it works is same to keep Class level information but it was part of Heap Memory

Metaspace introduced in Java 8 it is same as PermGen but it is not the part of Heap Memory altough it is part of OS Memory

Why PermGen removed?

  • Because it is Fixed in Size
  • and Hard to tune means difficult to choose the right memory size and settings so that application rum smoothly
  • Application fail easily under Heavy Framework like Spring, Hibernate and hot deployment and getting OutOfMemoryError: PermGen space very frequently


And Metaspace has:

  • No fixed JVM limit
  • Grows automatically
  • Easier JVM tuning
  • Fewer Memory leaks


Metaspace can also throw OutOfMemoryError: Metaspace but is not so often, PermGen failed due to artificial limits; Metaspace fails only when there is a real problem. 


Stack Memory

An area to store Methods and its Variable, Stack stores information from .class files

A separate memory block gets created for each method which stores temporary variables and primitive data types in each thread and remains in thread only till methods exist, and gets deleted as soon as methods get completed in LIFO order (LAST IN FIRST OUT) 

It stores:

  • Method calls
  • Local variables
  • Partial results
  • Return addresses

Each thread has its own stack.

If Stack Fails: java.lang.StackOverflowError


PC Register stores Address of current instruction being executed, it helps JVM to know where execution paused and where to resume after context switching

If it fails then no direct error throw directly JVM crash but it is very rare

Example:

JVM executes methodA

PC Register moves step by step from methoA we are calling methodB

On calling methodB

PC Register jumps to methodB’s first instruction and from methoB we are calling methodC

On calling methodC

PC Register jumps to methodC

After methodC finishes

PC Register jumps back to methodB (next instruction)

After methodB finishes

PC Register jumps back to methodA (next instruction after the call)


Stack Remembers method call order

PC Register Remembers current instruction

Both together Enable correct method return flow


Native Method Stack

Stores non java code method calls like c or C++

Native Method Stack is used by JVM to execute native code written in languages like C or C++ via JNI.


If it fails then it can throw either java.lang.StackOverflowError or OutOfMemoryError


Question can comes into mind:

Ques. How JVM memory areas map to real RAM usage ?

Ans. Everything JVM uses ultimately comes from physical RAM, When you run your java application then OS creates one process and allocates Virtual Memory, JVM requests chunks of memory then OS just provides the memory it doesn't know anything Heap, Stack etc. 

OS Map Physical Memory of RAM with JVM's virtual memory because JVM use Virtual memory.


JVM just divides the RAM into parts for different purposes, Once JVM start OS provides RAM space, JVM creates multiple areas accordingly. OS only sees one JVM process not seperate areas.

In Simple Terms: JVM runs as a single OS process, requesting virtual memory from the OS, which is mapped to physical RAM; inside this memory, JVM internally divides it into Heap, Stack, Metaspace, and other areas, but the OS sees it as just one process.


Execution Engine: Execution Engine Phase



Comments

Popular posts from this blog

From Power ON → Firmware → OS Kernel → Process Creation → JVM → Bytecode Execution, every layer matters when you’re designing:

JVM-ClassLoader