Skip to content

_debugmallocstats() undercounts bytes in type-specific free lists #148189

Closed

Description

@tim-one

Bug report

Bug description:

Toward the tail end of sys._debugmallocstats() output there's an account of bytes sitting in various type-specific free lists, like so:|

           62 free PyDictObjects * 48 bytes each =                2,976
       77 free PyDictKeysObjects * 32 bytes each =                2,464
           6 free PyFloatObjects * 24 bytes each =                  144
           60 free PyListObjects * 40 bytes each =                2,400
 231 free 1-sized PyTupleObjects * 40 bytes each =                9,240
...

However, for the container types that participate in cyclic gc (dict, list, tuple), the "bytes each" figurers are too small. Like:

Python 3.14.1 (tags/v3.14.1:57e0d17, Dec  2 2025, 14:05:07) [MSC v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getsizeof([])
56

56 > 40, by 16. Same 16 missing for dicts and tuples. I suspect (but don't know) that they're missing the two pointers (before the PYObject's type pointer) used by cyclic gc to link together the members of a generation.

Labeling as "easy", because it has to 😉 be shallow. A good exercise for somene to sharpen their archeological skills.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    type-bug
    on Apr 6, 2026
  2. MazinSharaf commented on Apr 7, 2026

    @MazinSharaf
    Contributor

    Isn't the tuple supposed to remain unchanged though? And also why are you using AI to write the comment?

  3. tim-one commented on Apr 7, 2026

    @tim-one
    MemberAuthor

    I believe the analysis is correct, but the "fix" is off. The code should follow what the sys.siseof() implementation does instead. For lists, that's

    static PyObject *
    list___sizeof___impl(PyListObject *self)
    /*[clinic end generated code: output=3417541f95f9a53e input=b8030a5d5ce8a187]*/
    {
        size_t res = _PyObject_SIZE(Py_TYPE(self));
    #ifdef Py_GIL_DISABLED
        PyObject **ob_item = _Py_atomic_load_ptr(&self->ob_item);
        if (ob_item != NULL) {
            res += list_capacity(ob_item) * sizeof(PyObject *);
        }
    #else
        res += (size_t)self->allocated * sizeof(PyObject *);
    #endif
        return PyLong_FromSize_t(res);

    _PyObject_SIZE() already knows everything, and will be updated as needed if/when things change.

    @MazinSharaf. yes, tuples are immutable, but that isn't relevant. They still participate in cyclic gc, because they can appear in cycles, like

    >>> L = []
    >>> t = L,
    >>> t
    ([],)
    >>> L.append(t)
    >>> t
    ([(...)],)

    So tuple objects also have a GC head prefix.

  4. MazinSharaf commented on Apr 7, 2026

    @MazinSharaf
    Contributor

    Alright then I'll change calculation of the tuple in the PR I made.

  5. tim-one commented on Apr 7, 2026

    @tim-one
    MemberAuthor

    Good! The high-order bit is to use _PyObject_SIZE(Py_TYPE(self)), which already knows total object struct sizes, gc or not. It's a macro that reads up the tp_basicsize member of a type object. To check results, make sure "bytes each" output matches sys.getsizeof() output.

  6. added
    interpreter-core
    on Apr 7, 2026
  7. MazinSharaf commented on Apr 7, 2026

    @MazinSharaf
    Contributor

    But for Tuples it should be _PyObject_VAR_SIZE(Py_TYPE(self)) correct? Also view the PR for the changes I made. Right now for objects and dicts its a bit more manual, but it was just something to get started with to properly calculate the size.

  8. MazinSharaf commented on Apr 7, 2026

    @MazinSharaf
    Contributor

    Should be done anyways, I just did the same thing I did with the other list and dict objects and made it so that:
    _PyType_PreHeaderSize(&PyTYPE_Type) is added to the size(PyTYPEObject).

  9. added a commit that references this issue on May 3, 2026
  10. sergey-miryanov commented on May 6, 2026

    @sergey-miryanov
    Contributor

    Should we add tests for this?

  11. tim-one commented on May 6, 2026

    @tim-one
    MemberAuthor

    Tests are always good, but it's clumsy here: there is no programmatic interface, the function just prints English to stderr.

    At a higher level, these "free list" stats didn't belong here to begin with. I wrote the initial implementation, and it was intended to report on pymalloc's own internals. Type-specific free lists have nothing to do with that. They're independent of the object allocator in use.

    So better would be to refactor, so that free-list reporting would be in its own function. Then, e.g., builds that use mimalloc instead could also use it. The new function would be the place to add new tests of it.

    In any case, I don't intend to pursue this. Happy to see someone else do it, though 😉.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    easyinterpreter-coretype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions