5

I was trying to figure out how python store the reference count of an object :

getrefcount(...)
    getrefcount(object) -> integer

    Return the reference count of object.  The count returned is generally
    one higher than you might expect, because it includes the (temporary)
    reference as an argument to getrefcount().
    >>>

>>> s = 'string'
>>> sys.getrefcount(s)
28
>>> d = {'key' : s}
>>> sys.getrefcount(s)
29
>>> l = [s]
>>> sys.getrefcount(s)
30
>>> del l
>>> sys.getrefcount(s)
29
>>> del d
>>> sys.getrefcount(s)
28
>>>

In my above snippet, as soon as i create a string object s i got the ref-count 28, then when i assigned inside a dictionary its ref-count increment by one. And I don't know why it starts with 28.

So, Here I am trying to figure out where this values store or how python get it.

Thanks

9
  • 2
    Constant string can be shared if it's used somewhere with same data. If you try to use very specific string such as 'whollala' maybe it's reference count should be different. Commented Feb 14, 2014 at 9:23
  • Ok but where does this reference counter is stored, it is inside the object attribute or python use some kind of data structure to track every object creation. Commented Feb 14, 2014 at 9:24
  • Random strings like 'asdsgfds' have the expected reference count(2). Commented Feb 14, 2014 at 9:24
  • oh why 2 is expected ? Commented Feb 14, 2014 at 9:26
  • Because there are only 2 visible references to it. Commented Feb 14, 2014 at 9:27

3 Answers 3

5

You can get the list of referrers to your object, using gc.get_referrers function, like this

import gc, pprint
pprint.pprint(gc.get_referrers("string"))

The reference count of each and every object is stored in the object itself, in a variable called ob_refcnt

typedef struct _object {
    _PyObject_HEAD_EXTRA
    Py_ssize_t ob_refcnt;
    struct _typeobject *ob_type;
} PyObject;

And the Reference counts of the objects are incremented and decremented using the macros Py_INCREF and Py_DECREF respectively.

#define Py_INCREF(op) (                         \
    _Py_INC_REFTOTAL  _Py_REF_DEBUG_COMMA       \
    ((PyObject*)(op))->ob_refcnt++)

#define Py_DECREF(op)                                   \
    do {                                                \
        if (_Py_DEC_REFTOTAL  _Py_REF_DEBUG_COMMA       \
        --((PyObject*)(op))->ob_refcnt != 0)            \
            _Py_CHECK_REFCNT(op)                        \
        else                                            \
        _Py_Dealloc((PyObject *)(op));                  \
    } while (0)
Sign up to request clarification or add additional context in comments.

4 Comments

Ah this explain why its 28 ref count. But I want to know where those counter are stored, how can find that.
@pinkman Please check my answer. I have included the source code location which points to the actual variable where the reference count is stored.
Ooh, a source link. Nice.
thank you @thefourtheye i ll read that and will see how much i can understand it.
1

Python uses same object for string literal which have the same value. Maybe, that's why you can see unexpectedly high ref count in your case.

For example, if I try to set 'string' literal to multiple string object, as you can see 's' object reference count keep increasing.

>>> s = 'string'
>>> sys.getrefcount(s)
2
>>> a = 'string'
>>> sys.getrefcount(s)
3
>>> b = 'string'
>>> sys.getrefcount(s)
4

3 Comments

Any idea what would be referring to 'string'?
So, here we are creating an string object 'string' and bind to a name called 's', so now when we create that object how does python start counting the reference.
@JayanthKoushik: There is a module named string.
1

The reference count of an object is stored in the object itself, in the C-level ob_refcnt field of the ob_base field of the C struct representing the object. You can't access these fields, or at least not through anything like obj.ob_base.ob_refcnt.

The documentation for PyObject is a bit out of date. I believe PEP 3123 has a more recent description of the PyObject type and PyObject_HEAD macros, but that might be out of date too. I would provide a source link, but I have no idea where these things are defined in the source.

1 Comment

ah could you please provide me some docs or link, so i want to read more on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.