Samet Nangshe, Phang Nga, Thailand

Member-only story

Java Memory Model: A Comprehensive Guide

🚀 Backend Scaling Playbook
7 min readSep 16, 2023

--

Java Memory Model

The Java Memory Model (JMM) governs how threads in Java interact with memory. It guarantees that changes made by one thread become visible to others, providing a framework for safe multi-threading. JMM ensures proper synchronization through constructs like `synchronized` blocks, `volatile` variables, and memory barriers. It’s crucial for preventing data races and ensuring consistent behavior in multi-threaded Java programs. Understanding JMM is fundamental for writing reliable and efficient concurrent code

Thread Stacks

Each thread running within the Java virtual machine has its own thread stack. Local variables for primitive types are fully stored on the thread stack and are not visible to other threads. Even if two threads are executing the same code, they will create their own separate copies of local variables for that code in their respective thread stacks

How Thread Stack is created?

  1. When the thread is started, the Java runtime environment creates a new thread stack for the thread. The thread stack is initially empty.
  2. When the thread calls methodA(), the runtime environment pushes a new frame onto the thread stack. The frame contains the call stack and local variables for methodA().
  3. When methodA() calls methodB(), the runtime environment pushes another frame onto the thread stack. The frame contains the call stack and local variables for methodB().
  4. When methodB() returns, the runtime environment pops the frame for methodB() off the thread stack. When methodA() returns, the runtime environment pops the frame for methodA() off the thread stack.

Key Features of Stack Memory

  1. Dynamic Growth and Shrinkage: Stack memory grows and shrinks as new methods are called and returned, respectively. When a method is called, a new stack frame is created, and when the method returns, the corresponding stack frame is removed.

2. Limited Lifetime: Variables declared in the stack exist only as long as the method that created them is running. Once the method finishes execution, the stack frame and its local variables are automatically…

--

--

🚀 Backend Scaling Playbook
🚀 Backend Scaling Playbook

Written by 🚀 Backend Scaling Playbook

Scalable Distributed System, Backend Performance Optimization, Java Enthusiast. (mazumder.dip.auvi@gmail.com Or, +8801741240520)

Responses (2)

Write a response