NOTE
Garbage Collection Concepts
Reference counting, reachability analysis, GC Roots, concrete garbage collection algorithms, and references.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Reference Counting
Each object has a counter. The counter is incremented by 1 when a variable references it and decremented by 1 when a reference becomes invalid. When the counter reaches 0, the object is reclaimed.
Advantage: simple and efficient.
Disadvantage: it cannot solve the circular-reference problem.
A circular reference means that object A references object B and object B references object A, but neither A nor B is referenced by any other object.
2. Reachability Analysis
Starting from GC Roots, traverse the object graph through reference relationships. Objects that can be reached are not garbage.
- GC Roots
- Objects referenced by local-variable tables in virtual-machine stacks.
- Objects referenced by native-method stacks.
- Objects referenced by static properties in the method area.
- Objects referenced by final properties in the method area.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub