Garbage collection tự động giải phóng bộ nhớ của các object không còn được tham chiếu.
- GC chạy khi áp lực bộ nhớ tăng cao, hệ thống sắp hết RAM, hoặc khi gọi tường minh
GC.Collect(). - Các object không còn accessible từ root references (stack, static fields) sẽ trở nên eligible để thu gom.
- GC là non-deterministic — dùng
IDisposablecho tài nguyên cần cleanup ngay lập tức.
Garbage collection automatically frees memory from unreachable objects.
- The GC runs when memory pressure increases, system memory runs low, or
GC.Collect()is called explicitly. - Objects unreachable from root references become eligible for collection.
- GC is non-deterministic — use
IDisposablefor resources requiring immediate cleanup.