C API: Testing my reference counting

Tim Peters tim.peters at gmail.com
Thu Mar 23 06:22:13 CET 2006

[lord trousers]
>>> Is there a way I can get hold of these kinds of statistics for
>>> debugging?

[Martin v. Löwis]
>> This is best done when Python is build in debug mode.
>> sys.gettotalrefcount then gives you the number of INCREF
>> calls for which no DECREF has been made; you said that
>> this shouldn't change.
>> If it does change, sys.get_counts() will give you the
>> number of objects per type.
>> Furthermore, sys.getobjects() will give you a list of
>> all objects allocated (excluding the result list).

[lord trousers]
> Wonderful! That's just what I was looking for.
> Is this kind of thing documented somewhere public? (As attributes that
> only show up in the debug build, they aren't documented in the regular
> library docs.) There might be more nifty goodies like this, and I'd
> like to check them out.

Actually, sys.getcounts() only exists in a COUNT_ALLOCS build (which
can be combined with a debug build, but is not implied by a debug

All that (and other esoterica) is documented in
Misc/SpecialBuilds.txt, in any Python source distribution.  The Python
Windows installer does not contain that file.

More information about the Python-list mailing list