liveBook · Manning

Garbage collection

As explained in Troubleshooting Java, Second Edition

Garbage collection is the automatic runtime process of reclaiming memory occupied by objects that are no longer reachable.

Garbage collection (GC) is the JVM mechanism that automatically removes objects the application no longer needs. It can reclaim unreachable objects, but it cannot remove objects that live references still retain. section 5.1.1 section 5.2.2

Treat GC as both a memory-management service and an investigation signal. Excessive GC activity, long pauses, or heap usage that remains high after collection can indicate inefficient allocation, a memory leak, poor tuning, or insufficient memory. section 11.4.1 section 11.4.3

Investigate GC-related problems by asking:

  • Are collections frequent, and are pause durations consistently high rather than isolated exceptions? section 11.4.1
  • Does a full collection reclaim little memory, suggesting that objects remain reachable longer than expected? section 11.4.1 section 5.1.1
  • Does heap usage grow over time even though collections continue, or does GC reclaim memory effectively while the workload still exceeds capacity? section 11.4.2 section 11.4.3
  • Does VisualVM show high GC CPU usage alongside allocation pressure, or high application CPU with little GC activity? section 5.2.2

Distinguishing a leak from insufficient memory

Suppose repeated GC events reduce the heap from 1,200 MB to 1,050 MB, then a full GC reduces it only to 1,148 MB. Persistent retention, such as uncleared session references in a collection, points toward a leak. By contrast, repeated full collections can reclaim memory successfully while the heap repeatedly approaches its limit; that pattern may mean the workload needs more memory, a more efficient design, or horizontal scaling. section 11.4.2 section 11.4.3

[2024-02-01T12:00:01.123+0000] GC(45) 
  ➥ Pause Young (G1 Evacuation Pause) 0.025s
[2024-02-01T12:00:02.456+0000] GC(46) 
  ➥ Pause Young (G1 Evacuation Pause) 0.027s
[2024-02-01T12:00:05.789+0000] GC(47) 
  ➥ Pause Young (G1 Evacuation Pause) 0.026s
[2024-02-01T12:00:10.012+0000] 
  ➥ GC(48) Pause Young (G1 Evacuation Pause) 0.110s
[2024-02-01T12:00:20.345+0000] 
  ➥ GC(49) Pause Young (G1 Evacuation Pause) 0.250s
[2024-02-01T12:00:35.678+0000] 
  ➥ GC(50) Pause Full (System.gc()) 2.567s
[2024-02-01T12:00:50.901+0000] 
  ➥ GC(51) Pause Young (G1 Evacuation Pause) 0.180s
[2024-02-01T12:01:10.234+0000] 
  ➥ GC(52) Pause Full (Allocation Failure) 5.321s

Use the GC-log listing to compare ordinary young collections with increasingly long pauses and full collections, then check whether the pattern is recurring. section 11.4.1

How Troubleshooting Java, Second Edition covers it

How deep it goes
Explains reachability, leak symptoms, log interpretation, and diagnostic distinctions, while deferring root-cause confirmation to heap-dump analysis.
Examples
Uses VisualVM widgets, GC-log listings, heap-growth patterns, and persistent session references to illustrate diagnosis.
What you can do after
Diagnose/debug with VisualVM and GC logs by correlating pause times, frequency, heap reclamation, and CPU usage.
How it connects
Links garbage collection with memory leaks, heap memory, GC logs, profiling tools, heap dumps, performance tuning, and OutOfMemoryError.

Also explained in

Compare how 9 books explain garbage collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.