Skip to content
UPEEC

Reference Counting

Each object counts how many references point to it and is freed when the count reaches zero.

Intermediate
Comments 0
import sys
a = []
sys.getrefcount(a)   # at least 2 (a + the function argument)
b = a                # count +1
del a                # count -1; object still alive through b

Deterministic freeing is why files are often closed promptly in CPython — but use with instead of relying on it.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.