[Python-Dev] PEP 42: sizeof(obj) builtin

Tim Peters tim.one@comcast.net
Sun, 02 Feb 2003 22:42:20 -0500


[Samuele Pedroni, on sizeof()]
> it is very unlikely that we can implement this in Jython,

I'm sure that you could, but no better than CPython:  return a lower bound
that may or may not be within a factor of 1000 of reality.  CPython could do
C sizeof() on Python objects, but that's about it.  There's no clear way to
know how much overhead is due to pymalloc "per object", and no way at all to
know how much overhead is imposed by the system malloc, or by the OS.

Continuing Jeremy's example:  How much memory does an empty Python dict
consume?  It's certain that *some* empty dicts grab at least 256KB from the
system malloc, because that's the minimum chunk size pymalloc uses for its
object arenas.  Telling a user that the unlucky empty dict triggering a new
arena allocation consumed some 128 bytes is a factor of 2000 off from what
was actually gotten from the C library.

There are many layers of memory, and we have a poor handle on most of them
even in CPython.  In a debug Python build, setting the envar
PYTHONMALLOCSTATS displays a lot of detail about pymalloc's memory use, but
that's only part of the story.

Implement the Jython sizeof() to always return 8, and who'd know the
difference <wink>.