-Xms10m -Xmx10m -XX:+HeapDumpOnOutOfMemoryError
Execute the following routine to continuously allocate objects within an unbounded collection until the memory ceiling is breached:
import java.util.ArrayList;
import java.util.List;
public class MemoryPressureTest {
private static class DataBlock {}
public static void triggerExhaustion() {
List<DataBlock> allocationQueue = new ArrayList<>();
while (true) {
allocationQueue.add(new DataBlock());
}
}
}
Running the pressure test produces a terminal failure indicating insufficient contiguous space, followed by automatic archive creation:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3512)
at java.util.ArrayList.grow(ArrayList.java:237)
at com.example.MemoryPressureTest.triggerExhaustion(MemoryPressureTest.java:13)
A .hprof file containing the live heap state is written to the execution directory immediately before process termination.
Import the generated snapsoht into Eclipse Memory Analyzer (MAT). Navigate to Histogram mode to inspect class distribution by retained heap size. The dominant contributor will typically be DataBlock alongside the underlying byte arrays used by ArrayList. Switch to the Dominator Tree view to visualize retention paths. You will observe the allocationQueue local variable maintaining strong references to thousands of DataBlock instances, preventing the garbage collector from reclaiming memory. Tracing these references back to GC roots confirms the unbounded growth pattern as the root cause of the OutOfMemoryError.