JVM จัดการหน่วยความจำโดยอัตโนมัติ ทั้งจัดสรรอ็อบเจกต์และคืนหน่วยความจำที่เข้าถึงไม่ได้ผ่าน garbage collection (GC) ดังนั้นคุณจึงไม่ต้องคืนหน่วยความจำเอง การเข้าใจส่วนต่าง ๆ ของหน่วยความจำและพฤติกรรมของ GC เป็นกุญแจสำคัญในการปรับจูนประสิทธิภาพและวินิจฉัยปัญหาหน่วยความจำ
ส่วนต่าง ๆ ของหน่วยความจำ JVM
HEAP — where all OBJECTS live (shared across threads), GC operates here
├── Young Generation — new objects (most die young)
│ ├── Eden — new allocations go here
│ └── Survivor — objects that survived a young GC
└── Old (Tenured) Gen — long-lived objects (promoted from young)
STACK — per-thread; method frames, local variables, primitive locals, references
Metaspace — class metadata (replaced PermGen in Java 8+)
