Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sys.getsizeof() does not include inline values #128762

Open
@hashbrowncipher

Description

@hashbrowncipher

Bug report

Bug description:

Commit tested: 5e65a1a

I have a program that allocates numerous instances of this type:

@dataclass
class Node:
    cdr: Self | None

BIG_DATA = None
for i in range(50):
    BIG_DATA = Node(BIG_DATA)
    print(sys.getsizeof(BIG_DATA))

After the 28th iteration, I find that it stabilizes at printing 48. But if I run the program in GDB and trace the actual allocations, I see that it is allocating 80 bytes per iteration:

# Set a GDB-recognized breakpoint
os.kill(os.getpid(), signal.SIGTRAP)
# In GDB:
# b _PyObject_Malloc
# commands
#  silent
#  print nbytes
#  continue
# end
for i in range(50):
    BIG_DATA = Node(BIG_DATA)

I believe that the missing bytes can be accounted for in the inline values. I observe the same behavior on Python 3.13.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions