-
Notifications
You must be signed in to change notification settings - Fork 3
Java Memory Model
kgleong edited this page Sep 10, 2015
·
2 revisions
- References
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 variables
- 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.
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?
- CPU reads data from main memory into its cache
- CPU may read data into its internal register from its cache
- CPU performs operations on the data
- CPU writes data back to the cache if necessary
- 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.
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.