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

Jeremy Hylton jeremy@zope.com
Fri, 31 Jan 2003 11:05:43 -0500


>>>>> "GvR" == Guido van Rossum <guido@python.org> writes:

  >> > I'm about to start working on this one and wanted to check here
  >> > first to make sure there is still a demand for it [...]
  >>
  >> Yes, please!  I have wished for this many times as I struggled to
  >> figure out which part of my program was using up too much RAM.

  GvR> But the question is incredibly ill-defined, so it's not clear
  GvR> that Raymond will be able to calculate a useful answer.  (See
  GvR> Tim's post.)

We've gone through the exercise of figuring out how much memory is
used by a dictionary several times in the office here.  It's
instructive to repeat the exercise, because it illustrates how hard it
is to come up with a good answer :-).

The basic dictionary structure is 132 bytes on i686, but the hash
table is (usually) a separately malloced chunk of memory that contains
the dict entries.  The details of the actual table layout aren't of
interest in dict_traverse(), which just gets the valid dict entries,
but the layout would be of interest to something like sizeof().

If the sizeof() function were going to really be useful, builtin types
would presumably need to define a slot to compute their size.  Even
this would overlook malloc overhead, which could well be nontrivial if
an object contains several malloced buffers.

Jeremy