all Python 3sysfunction
sys.getrefcount()
memory
Returns an object's reference count. The number is usually one higher than expected: the call's own argument is a reference too.
Syntax
sys.getrefcount(object)
For a = [] the answer is 2, not 1 — getrefcount's parameter holds the second reference. Since Python 3.12 immortal objects (None, True, small ints, many strings) have a huge count that means nothing. Only 0 and 1 are reliable; free-threaded builds and 3.14 optimizations change the numbers too. It is a CPython detail: hunt leaks with gc and tracemalloc instead.
Examples
import sys
a = []
print(sys.getrefcount(a)) # → 2
b = a
print(sys.getrefcount(a)) # → 3
del b
print(sys.getrefcount(a)) # → 2
# Python 3.12+
import sys
print(sys.getrefcount(None) > 10 ** 9) # → True — бессмертный объект
See also
Open in the full glossarydocs.python.orgReport an inaccuracy