Advanced Java and JVM· 35 min

JVM Architecture and Memory

See how class files become running code across the JVM, heap, stack, metaspace, and native memory.

Java interview lesson
Question 7 · Not reviewed

Advanced Java · runtime

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

JVM Architecture and Memory

Explain it aloud in your own words

Strong answer

A running Java process includes JVM-managed memory and native resources. Objects generally live in the heap, each thread has its own stack, class metadata lives in metaspace, and native allocations may sit outside the heap. The JVM specification describes behavior, while a particular JVM implementation decides many memory details.

Key points

  • A StackOverflowError is different from an OutOfMemoryError.
  • A memory leak in Java usually means reachable objects are retained unnecessarily.
  • The heap is shared; a thread's stack contains its frames and local variables.
  • Use measurements and diagnostic tools instead of guessing from source code.

Interviewer looks for

Separate specification guarantees from HotSpot implementation details and distinguish memory exhaustion from a logical retention leak.

Java example

1final class UserCache {
2  private final Map<String, User> values = new ConcurrentHashMap<>();
3  void put(String id, User user) { values.put(id, user); }
4  // Production code also needs an eviction or lifecycle policy.
5}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Intermediate

What is stored in the heap, stack, and metaspace?

Strong answer

The shared heap stores ordinary objects, each thread's stack stores call frames and local execution state, and metaspace stores class metadata in common HotSpot implementations. Native memory also exists outside these areas.

Q2Intermediate

How does StackOverflowError differ from OutOfMemoryError?

Strong answer

StackOverflowError commonly means a thread exhausted its stack through deep or infinite calls. OutOfMemoryError means the JVM or a native allocation could not obtain required memory, which may involve the heap, metaspace, or another region.

Q3Intermediate

What is a Java memory leak if garbage collection exists?

Strong answer

It is unnecessary retention of objects that remain reachable from GC roots, so the collector cannot reclaim them. Common causes include unbounded caches, static collections, listeners, and forgotten ThreadLocal values.

Q4Intermediate

What are GC roots?

Strong answer

GC roots are starting points the collector treats as live, such as active thread stacks, static fields, JNI references, and monitor references. Objects reachable from roots are considered live.

Q5Intermediate

Why should JVM memory problems be diagnosed with measurements?

Strong answer

The source-level symptom may be far from the cause: high allocation, retained objects, native buffers, thread stacks, or class metadata can all look like memory pressure. Heap dumps, native memory tracking, and runtime metrics separate those cases.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.