Huge value returned for Python reference count
What are reference counts
Reference counting is a programming technique for storing the number of references to an object. Reference counts can be used to de-allocate resources (e.g. memory) corresponding to the objects in garbage collection algorithms. As soon as an object is no longer referenced (i.e. reference count reduced to 0), resources allocated for the object can be reclaimed immediately, and put back into the resource pool.
How to retrieve a reference count
Python provides an easy way to retrieve the reference count for a given object, using the getrefcount() function in sys module. See an example in below:
>>> x = 333333
You may wonder why the returned count value is 2, instead of 1. It is because besides the 1st assignment line for variable
x, the sys.getrefcount(x) line also increments the reference count by 1, as it refers to x as its function argument.
Why do I get a huge value
Sometimes you can get a huge value, like in the example below:
>>> xxx = 1
It’s not the Python interpreter goes crazy, but actually an effect of CPython compiler optimization — a single object is kept in memory for an immutable literal (e.g. integer “1”, string “foo”), and let variables point to the immutable literal when the relevant value is set. As we know “1” is a commonly used value while setting integer variables, it’s probably not hard to imagine there are thousands of places in the Python source code that have that common value set, with each triggers the reference count incremented by 1, before xxx = 1 is typed in above Python interpreter.
If you look back the 1st example in this article where of x = 333333, you’ll see the reference count started with 1, since the “333333” is a rarely used literal value.
The below stackoverflow article provides a good answer on this topic: