Garbage Collection
Garbage collection automatically detects and reclaims memory occupied by unreachable objects, freeing developers from manual memory management.
Purpose
Automatic memory management prevents leaks and dangling pointers, improving safety and development efficiency.
Mechanism
Mark-sweep: traverse from the root set to mark reachable objects, then sweep the unmarked. Generational collection: young generation (collected frequently) + old generation (collected occasionally). V8 uses Scavenge (young) + Mark-Sweep/Compact (old).
Use cases
- Memory management
- Language runtimes
- Performance tuning