
Java Memory Model and Happens Before Explained With Examples
What the Java Memory Model guarantees about visibility and ordering, how happens-before edges are created and why unsynchronized code can loop forever.
Tag
66 posts

What the Java Memory Model guarantees about visibility and ordering, how happens-before edges are created and why unsynchronized code can loop forever.

Map every region of memory a JVM process uses, from the heap and thread stacks to Metaspace and direct buffers, and measure each with Native Memory Tracking.

Fix a Java container killed with OOMKilled and exit code 137 by budgeting heap, metaspace, threads and native memory inside the container limit.

Why ClassCastException appears where no cast is written, how raw types and unchecked warnings cause heap pollution, and how to fix it at the source.

Recognize a Java deadlock in a thread dump, find the two locks and threads involved, and remove the cycle with lock ordering, timeouts or smaller locks.

Understand JIT tiered compilation: the interpreter, C1 and C2 tiers, profiling, inlining, deoptimization, warm up and the flags that matter.

Why changing a list inside a for-each loop throws ConcurrentModificationException, and the safe ways to remove, add or replace elements while iterating.

Fix RejectedExecutionException by telling a full queue from a shut down executor and choosing a pool size, queue bound and rejection policy on purpose.

How the JVM finds and frees unreachable objects, why most collectors are generational, how G1 and ZGC differ and what pause and throughput really mean.

Fix the JVM start-up error about reserving heap space by checking 32-bit JVMs, a heap larger than memory, container limits and fragmented address space.

Read large JDBC result sets without running out of memory: setFetchSize, streaming cursors in PostgreSQL and MySQL, and transaction needs.

Fix java.lang.OutOfMemoryError Java heap space by deciding whether the heap is too small or something is leaking, using a heap dump to tell the two apart.