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
>>> sys.getrefcount(x)
2
>>> sys.getrefcount(x)
2

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
>>> sys.getrefcount(xxx)
7462

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:

--

--

--

OpenSource & Automation make me excited. Release engineering @MongoDB

Love podcasts or audiobooks? Learn on the go with our new app.

Tips on using the Neo4j Docker Container

Implement dark mode using flutter provider

What is Code Review?

Code-review

The Benefits of Materialized Views (and how to use them in your Ruby on Rails project)

Some Scrum FAQs

ls -l in Bash

Terraform — Safely upgrading RDS instances

Slack Lunch Club, Part 5/7: Testing and Deployment

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luke Chen

Luke Chen

OpenSource & Automation make me excited. Release engineering @MongoDB

More from Medium

dbSNP (Single Nucleotide Polymorphism Database)

Python Unit-Testing

Well Euled Machines

5 Machine Learning examples from your daily life you didn’t know about