Help: sizeof() and reference in python?

Kragen Sitaker kragen at
Mon Mar 6 21:01:30 CET 2000

In article <LPBBLPGHGGJMCPPOKONLIEPICFAA.gchiaramonte at>,
Gene Chiaramonte <gchiaramonte at> wrote:
>I guess I should have been more specific.
>1 - I am trying to get the size of an object in bytes to monitor how much
>memory a list or object occupies.

OK.  Suppose I have

x = 'x' * 1048576

Now x is one megabyte.  Now suppose I have

y = [x, x, x, x]

Now, what is the size of y?  There are at least a few plausible answers:
a- it's under 100 bytes; if I clear y, I will only have less than 100
   bytes more free memory.  
b- it's under 100 bytes; y itself only occupies under 100 bytes, although it
   has pointers to a much larger object.
c- it's one megabyte, plus a little bit: the total size of all objects 
   reachable from y.
d- it's four megabytes: the total size of the elements of y.

Now, if I say

x = 0

answer (a) changes (it suddenly becomes the same as answer (c)), but
answer (b) doesn't.

Suppose now we say:

z = [y[0]]

Now, according to answer (a), z is under 100 bytes, and so is y --- but
together, they are a little over a megabyte, by the same reasoning.

Garbage collection definitely has its pluses, but ease of understanding
where your memory went is not one of them.

>2 - I am trying to get the pointer to the memory address of an object. Much
>like the array objects buffer_info(), where the second item in the returned
>tuple is the pointer to the memory address of the first element of the

What do you want to do with it?
<kragen at>       Kragen Sitaker     <>
The Internet stock bubble didn't burst on 1999-11-08.  Hurrah!
The power didn't go out on 2000-01-01 either.  :)

More information about the Python-list mailing list