C

Core Java tutorial for beginners

Clean • Professional

Java Memory & Execution Model – Overview (JVM, Stack, Heap, Garbage Collection)

2 minute

Java Memory & Execution Model - Overview

The Java Memory & Execution Model defines how Java programs are loaded, executed, and managed inside the Java Virtual Machine (JVM). It also specifies how memory is allocated, accessed, and shared, which is crucial for thread safety and performance.

Two key components:

learn code with durgesh images

  1. JVM (Java Virtual Machine) – Executes Java bytecode.
  2. JMM (Java Memory Model) – Defines rules for memory visibility, ordering, and atomicity in multithreaded Java programs.

Java Execution Flow

Java execution is typically divided into four stages:

  1. Source Code (.java)
    • Written by the developer.
  2. Compilation (javac)
    • Converts source code into bytecode (.class).
  3. Class Loading (JVM)
    • ClassLoader loads classes into method area.
  4. Execution (JVM Runtime)
    • Interpreter or JIT compiler executes bytecode.
    • Runtime data areas (stack, heap, method area) are used during execution.

JVM Memory Areas

The JVM divides memory into several regions:

Memory AreaPurposeNotes
HeapStores objects & class instancesGarbage-collected, shared among threads
StackStores local variables, method callsThread-specific, LIFO
Method Area (PermGen/Metaspace)Stores class metadata & static variablesShared, loaded by ClassLoader
PC RegisterStores address of current instructionThread-private
Native Method StackStores native method callsOptional, thread-private

Stack vs Heap

FeatureStackHeap
StorageLocal variables, method framesObjects & arrays
LifetimeAutomatic, when method exitsUntil GC collects
AccessFast, thread-localSlower, shared
AllocationLIFODynamic

Java Memory Model (JMM)

The JMM defines how threads interact via memory:

  • Shared Variables: Heap objects are shared among threads.
  • Thread-Local Variables: Stack variables are private to threads.
  • Visibility: Changes made by one thread may not be immediately visible to others unless proper synchronization is used.
  • Atomicity: Some operations (like int read/write) are atomic; others require synchronized or volatile.
  • Ordering: Compiler/JVM may reorder instructions; volatile and synchronized provide happens-before guarantees.

Key JMM Concepts

  1. Volatile
    • Ensures visibility across threads.
    • Prevents instruction reordering.
  2. Synchronized
    • Ensures atomicity and mutual exclusion.
    • Establishes a happens-before relationship.
  3. Final Fields
    • Immutable fields are safely published once the constructor finishes.
  4. Happens-Before
    • A rule defining memory visibility between actions in different threads.

Execution & Memory Summary

  • JVM executes bytecode using stack frames and heap objects.
  • Heap stores objects; stack stores execution frames.
  • JMM ensures thread-safe access and predictable behavior in concurrent programs.
  • Garbage Collection automatically manages memory in the heap.

Advantages

  • Platform-independent execution
  • Automatic memory management
  • Safe multi-threaded memory access
  • High runtime performance
  • Reduced memory corruption and leaks

Real-World Applications

  • Enterprise applications (ERP, banking systems)
  • Backend servers & APIs
  • Android & mobile apps
  • Cloud-based microservices
  • High-performance computing systems

Article 0 of 0