Heap stack and memory areas
What you will learn
Memory management begins with ownership. A local variable may refer to a heap object that outlives the creating method. Memory measurements describe different parts of the runtime.
- Explain aliasing and object lifetime.
- Distinguish used, committed and maximum heap from process memory.
- Identify non-heap and direct-buffer costs.
Each example is a complete Main.java file. Use a fresh lab-owned directory: javac --release 21 -proc:none Main.java, then java Main. Use only trusted local classes and fixed synthetic data. Never run these diagnostics against another person’s process. The runner enforces a process deadline.
Return and alias a heap object
public class Main { static int[] make() { return new int[]{3, 5}; } public static void main(String[] args) throws Exception { int[] first = make(); int[] alias = first; alias[0] = 9; System.out.println(first[0]); }}Expected output:
9The local name in make disappeared, but the array remains accessible through the returned reference.
Allocation and runtime areas
Instances and arrays conceptually occupy the shared heap. Each method frame has local variables and an operand stack; a local reference may point to a shared object. Returning a reference can extend the object’s lifetime beyond the creating frame. HotSpot may eliminate physical allocations, so new does not guarantee an allocation event. Native thread stacks, metaspace, JIT code cache and direct buffers also consume memory.
Read memory numbers carefully
MemoryMXBean.getHeapMemoryUsage() reports used, committed and maximum bytes (maximum can be undefined, represented by -1). Committed is capacity guaranteed available to the JVM for that area; it is not the same as resident physical memory. Runtime.maxMemory() concerns the heap rather than the whole process. -Xmx bounds the Java heap, not RSS or container consumption. Do not compare separately sampled numbers as if captured atomically.
Budget rather than guess
Ten arrays of 1024 bytes contain 10240 payload bytes, before headers, alignment and references. A direct buffer can hold memory outside the normal heap; its Java wrapper still has a lifetime. Native memory tracking is an optional HotSpot diagnostic configured at startup and does not account for every native allocation. Leave capacity for non-heap memory and the operating system. Never induce OOM to demonstrate the point.
Read one heap snapshot
public class Main {
public static void main(String[] args) throws Exception { var usage = java.lang.management.ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); System.out.println(usage.getUsed() >= 0); System.out.println(usage.getCommitted() >= usage.getUsed()); System.out.println(usage.getMax() == -1 || usage.getMax() >= usage.getCommitted()); }}Expected output:
truetruetrueTest relationships within one snapshot, not environment-specific byte counts. These numbers do not describe the process RSS.
Direct and heap buffers
public class Main {
public static void main(String[] args) throws Exception { var heap = java.nio.ByteBuffer.allocate(16); var direct = java.nio.ByteBuffer.allocateDirect(16); heap.putInt(7); direct.putInt(7); System.out.println(heap.isDirect()); System.out.println(direct.isDirect()); System.out.println(direct.getInt(0)); }}Expected output:
falsetrue7Both buffers expose the same integer API. Direct storage may live outside the normal garbage-collected heap. Neither buffer is a precise memory meter.
Common mistakes
Do not equate local references with stack-allocated objects, -Xmx with RSS, committed with live data, or allocateDirect with storage reclaimed at a predictable instant.
Practice
Allocate four byte arrays of 256 bytes each and compute their total payload capacity, not object size. Print 1024.
Expected output:
1024Compare your answer with the manual practice criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Heap stack and memory areas — knowledge check
Original memory and diagnostics questions.
What can survive a method return?
What does -Xmx limit?
Which value can be -1 in MemoryUsage?
What does an array length measure?
What is true of direct buffers?