Trung BìnhPython iconPython

Python quản lý bộ nhớ như thế nào?

Python dùng hai cơ chế:

  1. Reference counting — mỗi object đếm số tham chiếu, về 0 thì giải phóng ngay
  2. Garbage Collector (module gc) — phát hiện và thu hồi circular references mà reference counting không xử lý được
  3. 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.

Xem toàn bộ Python cùng filter theo level & chủ đề con.

Mở danh sách Python