Garbage collection
Garbage collection reclaims Java objects that are no longer reachable. You allocate with new. You do not free. The collector finds live objects and recycles the rest.
Detailed Explanation
Reachability is the rule. An object is live if a GC root can find it: a local variable on a thread stack, a static field, JNI, or another live object that refers to it. When the last path disappears, the object is garbage. The collector will reuse its memory. There is no promised time. finalize is deprecated and delayed; do not use it for cleanup. Use try-with-resources.
Collectors pause or slow application threads to walk the heap. Modern HotSpot collectors (G1 is the default, ZGC and Shenandoah exist for low pause) try to keep pauses short. Throughput collectors (Parallel) maximize batch work and pause longer. You pick with -XX:+UseG1GC and friends, usually by latency needs, not by habit.
Allocation is bump-the-pointer in thread-local allocation buffers for new objects. That is why new is cheap and why a burst of short-lived objects is normal Java. The young generation is collected often. Objects that survive are copied to survivor spaces and then to old generation. That is generational GC.
You reach for GC knowledge when the heap grows, when pause times miss an SLA, or when a leak keeps objects reachable (a static map, a forgotten listener, a ThreadLocal on a pool). You do not call System.gc() in application code; it is a hint and can cause a full collection.
The cost of GC is CPU and pauses, paid for memory safety and no use-after-free. The cost of a huge heap with a tiny live set can still be a long mark if the collector is wrong for the load. Tune after you measure (-Xlog:gc*, flight recorder). The cost of caching forever is a heap that looks "used" and never returns memory to the OS (policy-dependent).
OutOfMemoryError means the collector could not free enough. It is not "GC is broken"; it is "everything is still reachable, or the heap is too small".
Code Examples
public class Roots {
static Object pin; // static field: a GC root
public static void main(String[] args) {
Object local = new byte[16]; // stack root while the frame lives
pin = local;
local = null; // pin still keeps the array alive
System.out.println(pin != null); // prints: true
pin = null; // now the array is unreachable
}
}public class Alloc {
public static void main(String[] args) {
long before = Runtime.getRuntime().freeMemory();
byte[] chunk = new byte[1_000_000];
long after = Runtime.getRuntime().freeMemory();
System.out.println(chunk.length); // prints: 1000000
System.out.println(before != after); // prints: true (usually)
}
}Common Gotchas
Reachable is not "I am done with it"
a static collection or a listener list keeps objects forever.
`finalize` is not a destructor
it may run late or never. Use `AutoCloseable`.
`System.gc()` is a request, not a command
do not rely on it in production.
Allocation is cheap; retention is not
leaks show up as old-gen growth, not as `new` being slow.