HardConcept

How does JVM memory management and garbage collection work in Java?

Technology:
Java
Experience:
Mid-level,
Senior

Quick answer

The JVM divides memory into a per-thread stack, a shared heap (split into young and old generations) and Metaspace; the garbage collector frees heap objects that are no longer reachable, collecting short-lived young objects frequently and old objects less often.

Detailed explanation

Each thread has its own stack holding method frames with local variables and references. All objects live on the heap. New objects are allocated in the young generation (Eden and two survivor spaces); objects that survive several minor collections are promoted to the old generation. Class metadata lives in Metaspace, which replaced PermGen in Java 8.

An object is garbage when it can no longer be reached from a GC root such as a thread stack, static field or JNI reference. G1 is the default collector since Java 9 and balances throughput with pause times; ZGC and Shenandoah target very low pauses on large heaps. Typical production problems are OutOfMemoryError from leaks (static collections, listeners, ThreadLocals not cleared) and long pauses from undersized or oversized heaps. Diagnose them with GC logs, heap dumps and tools such as VisualVM or Eclipse MAT.

Key points

  • Stack per thread, heap shared, Metaspace for class data
  • Generational collection: young then old
  • Leaks are objects still reachable by mistake

Follow-up questions

  • What is a memory leak in Java and how do you find one?
  • What is the difference between G1 and ZGC?
  • JavaMedium

    What is the difference between an abstract class and an interface in Java?

    An abstract class can have instance fields, constructors and a mix of implemented and abstract methods but supports only single inheritance, while an interface defines a contract that a class can implement multiple times and, since Java 8, can include default methods.

    Junior 路 Mid-level 路 Object-Oriented Programming
  • JavaMedium

    What is the difference between ArrayList and LinkedList in Java?

    ArrayList is backed by a resizable array with O(1) index access and O(n) insertion in the middle, while LinkedList is a doubly linked list with O(n) index access and O(1) insertion once you hold the node; in practice ArrayList is faster for most workloads.

    Junior 路 Mid-level 路 Data Structures
  • JavaMedium

    What is the difference between checked and unchecked exceptions in Java?

    Checked exceptions (subclasses of Exception other than RuntimeException) must be caught or declared with throws, while unchecked exceptions (RuntimeException and Error) do not have to be handled and usually signal programming bugs.

    Junior 路 Mid-level 路 Programming Fundamentals
  • JavaMedium

    What are the main features of Java 8: lambdas, streams and Optional?

    Java 8 introduced lambda expressions, functional interfaces, the Stream API for declarative collection processing, Optional to represent missing values, default methods in interfaces, method references and the java.time date and time API.

    Junior 路 Mid-level 路 Functions & Scope
  • JavaMedium

    How does HashMap work internally in Java?

    A HashMap stores entries in an array of buckets; it computes the key's hash code to choose a bucket, resolves collisions with a linked list that becomes a red-black tree when a bucket grows large, and resizes when the load factor of 0.75 is exceeded.

    Mid-level 路 Senior 路 Data Structures