Get the reference count of an Object in Python

avatar

Borislav Hadzhiev

Last updated: Sep 8, 2022

banner

Photo from Unsplash

Get the reference count of an Object in Python #

Use the sys.getrefcount() method to get the reference count of an object, e.g. sys.getrefcount(obj1). The sys.getrefcount() method returns the reference count of an object. The count is one higher than expected because the method creates a temporary reference.

main.py
import sys obj1 = {'id': 1, 'name': 'bobbyhadz'} print(sys.getrefcount(obj1)) # 👉️ 2 obj2 = obj1 print(sys.getrefcount(obj1)) # 👉️ 3 my_dict = { 'employee': obj1, } print(sys.getrefcount(obj1)) # 👉️ 4

The sys.getrefcount method returns the reference count of the provided object.

The count is one higher than expected because the method creates a temporary reference to the supplied object.
main.py
import sys obj1 = {'id': 1, 'name': 'bobbyhadz'} print(sys.getrefcount(obj1)) # 👉️ 2

You can use the gc.get_referrers method if you need to get a list of the objects that directly refer to the given object.

main.py
import sys import gc obj1 = {'id': 1, 'name': 'bobbyhadz'} print(sys.getrefcount(obj1)) # 👉️ 2 # [{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f2e8290dde0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/home/borislav/Desktop/bobbyhadz_python/main.py', '__cached__': None, 'sys': <module 'sys' (built-in)>, 'gc': <module 'gc' (built-in)>, 'obj1': {'id': 1, 'name': 'bobbyhadz'}}] print(gc.get_referrers(obj1))

The first object is the current namespace and the second is the variable we declared.

The gc.get_referrers method returns a list of the objects that directly refer to the supplied object.

You can use the id function or the is operator to check if two variables refer to the same object.

main.py
import sys obj1 = {'id': 1, 'name': 'bobbyhadz'} print(sys.getrefcount(obj1)) # 👉️ 2 obj2 = obj1 print(sys.getrefcount(obj1)) # 👉️ 3 print(obj1 is obj2) # 👉️ True print(id(obj1)) # 👉️ 139709435242240 print(id(obj2)) # 👉️ 139709435242240 print(id(obj1) == id(obj2)) # 👉️ True

The obj1 and obj2 variables point to the same location in memory and refer to the same object.

The id() function can be used to get the identity of an object.

The function returns an integer, which is guaranteed to be unique and constant for the object's lifetime.

If I use the del operator to delete the obj1 variable, we will still have a reference to it through the obj2 variable.

main.py
import sys obj1 = {'id': 1, 'name': 'bobbyhadz'} obj2 = obj1 print(sys.getrefcount(obj2)) # 👉️ 3 del obj1 print(sys.getrefcount(obj2)) # 👉️ 2 print(obj2) # 👉️ {'id': 1, 'name': 'bobbyhadz'}

We used the del operator to delete the obj1 variable, so the ref count of obj2 decreased to 2.

The reference count is really 1, but the sys.getrefcount() method creates a temporary reference to the supplied object.

We can still access the contents of obj1 through the obj2 variable.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.