Python dùng hai cơ chế:
- Reference counting — mỗi object đếm số tham chiếu, về 0 thì giải phóng ngay
- Garbage Collector (module
gc) — phát hiện và thu hồi circular references mà reference counting không xử lý được - Memory Pool (PyMalloc) — tối ưu allocation cho objects nhỏ < 512 bytes
Pitfall: Circular reference giữa hai objects sẽ không bao giờ được reference counting giải phóng.