Fundamentals 22 min read

Overview of Java Garbage Collection and Memory Management

This article provides a comprehensive overview of Java's garbage collection mechanism, memory regions, object access methods, allocation strategies, GC algorithms, and the various collectors used in the HotSpot JVM, helping developers understand and optimize Java memory usage.

Java Captain
Java Captain
Java Captain
Overview of Java Garbage Collection and Memory Management

Java Garbage Collection Overview

Java's garbage collection (GC) mechanism is a key difference from C/C++, allowing developers to rely on automatic memory management without writing explicit cleanup code, thereby preventing most memory leaks and overflow issues.

Java Memory Areas

The JVM divides memory into several regions: the Program Counter Register (thread‑private), the JVM Stack (thread‑private), the Native Method Stack (thread‑private), the Heap (shared among all threads and the primary area for GC), the Method Area (shared, stores class metadata, constants, and compiled code), and Direct Memory (outside JVM control).

Object Access Methods

Object references involve three regions: the JVM stack, the heap, and the method area. Access can be performed via handle‑based indirection or direct pointers; HotSpot uses the direct‑pointer approach for speed.

Java Memory Allocation Mechanism

Heap allocation follows a generational model: Young Generation (Eden + two Survivor spaces) and Old Generation, with optional Permanent Generation (Method Area). Objects are initially allocated in Eden; when Eden fills, a Minor GC copies surviving objects to a Survivor space, and after several promotions they move to the Old Generation.

GC Mechanisms

Young Generation collection uses a stop‑and‑copy algorithm, while Old Generation typically uses a mark‑compact algorithm. The Method Area (PermGen) is reclaimed mainly for class metadata and constant pool entries, with optional class unloading controlled by flags.

Garbage Collectors

HotSpot provides several collectors: Serial (single‑threaded), ParNew (multithreaded young‑gen), Parallel Scavenge (throughput‑oriented), Serial Old, Parallel Old, CMS (Concurrent Mark‑Sweep for low pause times), and G1 (available from JDK 7). Each collector has specific JVM flags for selection and tuning.

Notes and References

The article compiles notes from various blogs and the book "Deep Understanding of the Java Virtual Machine"; readers are encouraged to verify details and consult the listed references for deeper study.

JavaJVMperformanceMemory ManagementGarbage CollectionGC Algorithms
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.