MediumJava Memory Model Architecture. Before we deep dive into java memory… | by Vignesh | Medium
Sitemap

Java Memory Model Architecture

4 min readAug 7, 2026

--

Press enter or click to view image in full size

Before we deep dive into java memory model architecture, if you don’t know what is java and how it works? please click here.

Before we start a small recap of last session,

Whenever we wrote a java code we stored with .java extension which is in human readable format. Then JavaC is a java compiler(translator) which translates the human readable code into byteCode which has the extension .class, this code only understand by the machine, then Execution Engine inside in JVM converts byte code into machine code.

Have you ever think about why we need JavaC(java compiler)? Imagine you are going to a hotel and you are ordering some foods but the waiter don’t know your language. So you need a translator. Like here machines don’t know human (human readable format) code, so java compiler translates to bytecode.

Class loader which loads all the byte code . Then Execution engine in inside the JVM converts byte code into machine code. JDK is used to develop and run Java programs. It includes the JRE. If you only want to run a Java program, the JRE is sufficient. JRE is platform dependent. JRE provide the environment for only run the code.

JDK and JRE platform dependent

Bytecode
│
Class Loader
│
Bytecode Verifier
│
Execution Engine
│
┌──────────────┐
│ Interpreter │
│ or │
│ JIT │
└──────────────┘
│
Machine Code

In java every variables, methods and class are stored in different memory areas. In this blog we are going to mainly focus, how java memory works and where the data is actually stored.

Runtime Data Area

At runtime, JVM creates different memory area to execute a program. These memory areas called Runtime Data Areas.

There are mainly five data areas, and they are categorized into two types based on how they shared and managed across threads.

  1. Thread shared Memory area( created once per JVM).
  2. Thread private Memory area( created once per thread).

Thread is a small unit of execution or flow of execution.

1. Thread shared memory area

These are created when JVM starts, and are shared across all threads running in the application.

i. Method area

stores,

  • class metaData
  • static variables
  • method information
  • byteCode
  • constant pool

why shared,

There is only one copy of a class, if 1000 MemoryArchitecture Object is created like

MemoryArchitecture (obj1), MemoryArchitecture (obj2) …. MemoryArchitecture (obj1000)

JVM doesn’t create 1000 copies of class, It creates only one copy of class inside the MethodArea.

Get Vignesh’s stories in your inbox

Join Medium for free to get updates from this writer.

All class Objects are use that class definition.

ii. Heap Memory (Largest Memory Area)

  • It stores, Objects, Arrays and Instance (Dynamic, non-static, object) variables.
  • Heap memory allocated at runtime
  • If two threads need the same object, both threads can access it. That why it is thread shared.
Thread 1
\
\
---> Heap Object
/
Thread 2

Garbage Collector :

Objects that are no longer referenced are automatically removed by the Garbage Collector (GC). But GC not inside Heap, it is a part of JVM.

2. Thread Private Memory are

These areas are created individually whenever a new thread is spawned. Thread private area ensures each thread has its own isolated execution context (own private workspace).

i. Java Stack

stores,

  • Method execution frames
  • Method parameters
  • Local variables
  • Object reference

Whenever a method called JVM creates stack frames.

Stack Memory

| |
| fun3()|
|-------|
| fun2()|
|-------|
| fun1()|
|-------|
  • Memory is automatically released after method execution.
  • Each thread has its own stack memory.

ii. Program counter (pc registers)

  • Stores the address of the currently executing instruction.
  • Each thread has its own pc register.
  • It is thread private which is created once per thread.

iii. Native Method stack (JNI methods)

  • Stores the information for native methods written in languages like (c/c++).
  • These native methods are called through JNI (Java Native Interface).
  • Used when java interacts with native libraries
  • JNI acts as a bridge between JVM and native libraries
         JNI(Java Native Interface)
JVM------------------------------------------->Native Libraries
package com.memorySpace;

//class MemoryArchitecture (class metaData) - is stored in MethodArea;
public class MemoryArchitecture {

//static variables are stored in MethodArea
//static variables only accessible static methods
static String name="Vignesh T";
static int age=20;


//(non-static,object,instance,dynamic) variables are stored in Heap Memory;
String studentName;
int studentId;

// Constructor metadata and bytecode are stored in the Method Area.
public MemoryArchitecture(String studentName, int studentId) {
this.studentName = studentName;
this.studentId = studentId;
}

//Instance method bytecode is stored in MethodArea
//non-static can access non-static variables
void display() {
System.out.println("Student : " + studentName);
System.out.println("Id : " + studentId);
}

//Static method bytecode is stored in MethodArea
//Static can access static variables
static void showDetails(){
System.out.println("Name :"+name);
System.out.println("age :"+age);
}

public static void main(String args[]){
MemoryArchitecture m1=new MemoryArchitecture("Vignesh T",1);
// Object(new MemoryArchitecture("Vignesh T",1)) is stored in Heap Memory.
// Reference variable m1 is stored in stack memory


//primitive local variable is stored in Stack Memory.
int marks=100;
System.out.println("Marks :"+marks);
}
}

--

--