Skip to content
kgleong edited this page Sep 10, 2015 · 2 revisions

What is the Java memory model?

Java Memory Model

Java thread stack and heap memory

The JVM divides memory between thread stacks and the heap.

What is a thread stack?

  • each thread has its own stack that consists of the following:
    • local variables
      • references to objects or the primitive data itself
    • each method on the call stack for that thread
  • local primitive variables on the stack are only visible to the thread that owns it.
  • local reference variables that point to objects on the heap are also only visible to the owning thread.

What is the heap?

  • The heap contains all objects created by the application.
  • Objects reside here regardless of which thread created them.
  • Primitive member variables are stored on the heap, while local primitives are stored on the thread stack.
  • Objects on the heap are visible to all threads.

stack and heap references

Computer Hardware Memory

Computer memory

CPUs

  • Modern computers have mutiple CPUs, where each CPU can run a single thread at a time.
  • CPUs contains registers, dedicated memory for each CPU, which the CPU can access very quickly.

CPU Cache

  • Each CPU usually has its own cache, which is faster than accessing main memory but slower than accessing its internal registers.
  • Some CPUs have multiple levels of cache (Level 1, Level 2, etc.).

How does a CPU read and write variables in main memory?

  1. CPU reads data from main memory into its cache
  2. CPU may read data into its internal register from its cache
  3. CPU performs operations on the data
  4. CPU writes data back to the cache if necessary
  5. CPU writes data from the cache to main memory as needed. This is called flushing the cache to main memory.

When does CPU cache flushing occur? This occurs when the cache is full and the data stored there is about to be overwritten by new data that's being loaded into the cache.

Flushing can also be triggered upon exiting a synchronized block as described in this Stack Overflow post.

The Java Memory Model and how it maps to Computer Hardware Memory

Java memory mapping to computer hardware memory

Variables referenced by the thread stacks and heap can potentially be cached in all levels of the computer's memory.

What kind of problems does this create?

  • Threads may lose visibility into updates made by other threads to shared objects.
  • Variables are susceptible to race conditions, where threads hold on to stale shared variable state, causing outdated reads or unsynchronized writes.
Clone this wiki locally