sys.getrefcount() returning very large reference counts

Question:

In CPython 3.11, the following code returns very large reference counts for some objects. It seems to follow pre-cached objects like integers -5 to 256, but CPython 3.10 does not:

Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for i in (-6, -5, 0, 255, 256, 257):
...    print(i, sys.getrefcount(i))
...
-6 5
-5 1000000004
0 1000000535
255 1000000010
256 1000000040
257 5
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for i in (-6, -5, 0, 255, 256, 257):
...    print(i, sys.getrefcount(i))
...
-6 5
-5 6
0 234
255 8
256 26
257 5

PEP 683 – Immortal Objects, Using a Fixed Refcount may be related, but it isn’t mentioned in What’s New in Python 3.11, nor is a change in sys.getrefcount() documented.

Anybody have knowledge about this change?

Asked By: Mark Tolonen

||

Answers:

This is not related to PEP 683, but it will probably be superseded by PEP 683 once PEP 683 has been implemented.

This refcount change was first introduced in a commit on December 13, 2021, which introduced a _PyObject_IMMORTAL_INIT macro:

#define _PyObject_IMMORTAL_INIT(type) 
    { 
        .ob_refcnt = 999999999, 
        .ob_type = type, 
    }

Before this change, small int refcounts were initialized to 1. This change made it so their refcounts were instead initialized to 999999999.

The relevant issue discussion is here, and the pull request for the commit is here.

The value of 999999999 seems to have been copied from a fairly arbitrary decision Guido van Rossum made while writing an unrelated tool. Quoting a message by Guido in the issue discussion:

I used 999999999 in deepfreeze.py to signify "immortal object". It has
been copied by others (small integers are essentially immortal too). I
wasn’t too sure that the refcount wouldn’t go below zero if the
interpreter is repeatedly finalized and reinitialized. Once we have
official immortal objects (see PEP-683) we should switch to that.

Answered By: user2357112
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.