JavaIntermediate#memory#jvm

Explain Java memory model: heap vs stack.

Stack stores method frames, local variables, and references — fast, thread-local, LIFO. Heap stores all objects and class instances — shared across threads, managed by the garbage collector. The JVM also has Metaspace for class metadata.

Example
void method() {
  int x = 5;              // stack
  Person p = new Person(); // reference on stack, object on heap
}

Related Questions

1
JavaIntermediate#gc#memory

What is garbage collection?

Open
2
JavaIntermediate#exceptions

What are checked vs unchecked exceptions?

Open
3
JavaBeginner#strings

What is the difference between String, StringBuilder, and StringBuffer?

Open