Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
memory
model
heap
stack

What is the Java memory model?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

A memory model is best thought of as the architecture that determines how different parts of memory (i.e., stack and heap) interact. The Java Virtual Machine (JVM) divides the memory into two logical units:

  1. Thread stack
  2. Heap

1. Thread stack

The thread stack is the part of the memory which stores the data specific to a thread. Every thread has its own thread stack. The thread stack stores local variables (both primitive and reference variables) of each method, ​whether the method belongs to an object or not.

When two separate threads call the same method, each thread creates its own copy of the local variables declared in the method; one thread cannot directly access the variables of another thread.

2. Heap

There is only one shared heap for the whole Java application; it stores the objects created and their member variables (both primitives and references). Any thread that has a reference to an object on the heap can access its member variables. Moreover, a single object on the heap can be referenced by the local variables of different threads.

svg viewer
Java Memory Model

RELATED TAGS

java
memory
model
heap
stack
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring