Fundamentals 32 min read

Mastering Java Garbage Collection: Goals, Tuning, and Collector Choices

This article explains Java HotSpot VM garbage‑collection ergonomics, detailing pause‑time and throughput goals, generational collection mechanics, default heap sizing, and how to select and tune Serial, Parallel, G1, and ZGC collectors for optimal performance.

Programmer DD
Programmer DD
Programmer DD
Mastering Java Garbage Collection: Goals, Tuning, and Collector Choices

Optimization Goals and Strategies (Ergonomics)

Java HotSpot VM can prioritize either maximum pause time or application throughput. When the preferred goal is met, the collector tries to maximize the other.

Maximum Pause‑Time Goal

Pause time is the duration the GC stops the application. The -XX:MaxGCPauseMillis option sets a target pause in milliseconds; the collector adjusts heap size and related parameters to stay below this target, which may reduce overall throughput.

Throughput Goal

Throughput measures the proportion of time spent running the application versus GC. It is set with -XX:GCTimeRatio=nnn, where the GC‑to‑application time ratio becomes 1/(1+nnn). For example, -XX:GCTimeRatio=19 aims for 5% GC time.

Footprint Goal

If both pause‑time and throughput goals are satisfied, the collector may shrink the heap until one goal can no longer be met. Minimum and maximum heap sizes are controlled with -Xms and -Xmx.

Garbage Collector Implementation

Generational Garbage Collection

An object is considered garbage when no live references exist, allowing the VM to reuse its memory. Simple algorithms that scan all reachable objects are too costly for large applications, so HotSpot uses generational collection, exploiting the observation that most objects die young (the weak generational hypothesis).

Typical object‑lifetime distribution shows a large “young” peak where most objects become dead quickly, while a few long‑lived objects occupy the right tail.

Generations

Memory is divided into generations: a young generation (Eden + two Survivor spaces) and an old generation. Minor collections reclaim only the young generation; major collections reclaim the entire heap.

Performance Considerations

Throughput is the percentage of time not spent in GC.

Latency is the pause impact on application responsiveness.

Metrics can be observed with -verbose:gc (alias for -Xlog:gc). Example log lines show before/after heap usage and pause durations.

Factors Influencing GC Performance

Total Heap

More total memory generally improves throughput because collections occur less frequently.

Heap Options Affecting Generation Size

-Xmx sets the reserved heap size; -Xms sets the initial committed size. The -XX:NewRatio option controls the old‑to‑young generation size ratio, while -XX:NewSize and -XX:MaxNewSize bound the young generation.

Give the VM as much memory as possible unless pause‑time is a problem.

Setting -Xms and -Xmx to the same value removes dynamic resizing.

Scale memory with CPU count for parallel allocation.

Available Collectors

Serial Collector (single‑threaded, best for small data sets or single‑CPU machines).

Parallel Collector (multi‑threaded, prioritizes throughput).

G1 Collector (concurrent, aims for low pause times with good throughput).

Z Garbage Collector (experimental, low‑latency, suitable for very large heaps).

Collector Selection Guidelines

Small data set (~100 MB) → Serial.

Peak throughput, no pause requirement → Parallel.

Response time more important than throughput → G1.

Very low latency or TB‑scale heap → ZGC.

Parallel Collector

Enabled with -XX:+UseParallelGC. It runs minor and major collections in parallel, can use -XX:ParallelGCThreads to set thread count, and supports automatic ergonomics via -XX:MaxGCPauseMillis, -XX:GCTimeRatio, and -Xmx.

Default Heap Sizing

If not specified, the VM sets initial heap to 1/64 of physical memory and maximum heap to 1/4. The young generation’s maximum space is roughly one‑third of the total heap.

[15,651s][info][gc] GC(36) Pause Young (G1 Evacuation Pause) 239M->57M(307M) (15,646s, 15,651s) 5,048ms
[16,162s][info ][gc] GC(37) Pause Young (G1 Evacuation Pause) 238M->57M(307M) (16,146s, 16,162s) 16,565ms
[16,367s][info ][gc] GC(38) Pause Full (System.gc()) 69M->31M(104M) (16,202s, 16,367s) 164,581ms

G1 Garbage Collector

G1 is the default collector. It divides the heap into equal‑sized regions, performs concurrent marking, and selects regions with the most reclaimable space to meet pause‑time targets while maintaining good throughput.

Heap Layout

Regions are classified as young (Eden or Survivor) or old. Allocation occurs in young regions; large objects may be allocated directly in old regions.

GC Cycle

G1 alternates between Young‑only phases (minor collections) and Space‑reclamation phases (mixed collections that also reclaim old regions). When old‑generation occupancy reaches a threshold, a concurrent start triggers marking, followed by Remark and Cleanup pauses.

Configuration Options

-XX:MaxGCPauseMillis to set pause‑time target.

-XX:G1NewSizePercent / -XX:G1MaxNewSizePercent to bound young generation size.

-XX:InitiatingHeapOccupancyPercent (or adaptive IHOP) to trigger concurrent cycles.

-XX:+G1EnableStringDeduplication to enable string deduplication.

Z Garbage Collector

ZGC is an experimental low‑latency collector (enabled with -XX:+UnlockExperimentalVMOptions -XX:+UseZGC). It performs all expensive work concurrently, aiming for pauses under 10 ms, and is suited for very large heaps.

Key Tuning Options

-Xmx to set maximum heap size.

-XX:ConcGCThreads to set the number of concurrent GC threads.

Other Considerations

Explicit GC

Calling System.gc() forces a full collection.

Class Metadata

Since JDK 8, class metadata resides in native memory (Metaspace). Its size can be limited with -XX:MaxMetaspaceSize.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMGarbage Collectionzgcperformance tuningg1-gcParallel GCGC Ergonomics
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

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.