Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getting the size of an object

24 views
Skip to first unread message

filox

unread,
Jun 18, 2007, 12:07:05 PM6/18/07
to
is there a way to find out the size of an object in Python? e.g., how could
i get the size of a list or a tuple?

--
You're never too young to have a Vietnam flashback


Brett Hoerner

unread,
Jun 18, 2007, 12:08:52 PM6/18/07
to
On Jun 18, 11:07 am, "filox" <filox_realmmakni...@yahoo.com> wrote:
> is there a way to find out the size of an object in Python? e.g., how could
> i get the size of a list or a tuple?

"Size" can mean a lot of things,

len(my_list)
len(my_tuple)

Although I have the feeling you mean "how many bytes does this object
take in memory" - and I believe the short answer is no.

Brett

filox

unread,
Jun 18, 2007, 3:48:36 PM6/18/07
to

"Brett Hoerner" <bretth...@bretthoerner.com> wrote in message
news:1182182932.9...@o61g2000hsh.googlegroups.com...

is there a long answer? what i want is to find out the number of bytes the
object takes up in memory (during runtime). since python has a lot of
introspection mechanisms i thought that should be no problem...

Brett Hoerner

unread,
Jun 18, 2007, 5:57:37 PM6/18/07
to
On Jun 18, 2:48 pm, "filox" <filox_realmmakni...@yahoo.com> wrote:
> is there a long answer? what i want is to find out the number of bytes the
> object takes up in memory (during runtime). since python has a lot of
> introspection mechanisms i thought that should be no problem...

There isn't an automatic way through the language afaik. I think
allocating memory in order to keep track of how much memory you have
allocated can begin to be a problem. And most people just don't care
down to each and every byte. :)

Some helpful information here:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/a7b9f3c03fb49aa/0e793beec82884f0?lnk=gst&q=size+object&rnum=4#0e793beec82884f0

Brett

Lenard Lindstrom

unread,
Jun 18, 2007, 6:54:01 PM6/18/07
to

New-style classes have both a __basicsize__ and an __itemsize__
attribute. __basicsize__ gives the number of bytes in the fixed size
portion of an instance. For immutable types with variable size, such as
tuple and str, multiply __itemsize__ by the object length and add to
__basicsize__ to get the instance size. long type instances also vary in
length, but since long has no length property trying to figure out the
size of a particular instance is harder. And types like list, map and
unicode keep pointers to blocks of memory allocated separately.


--
Lenard Lindstrom
<le...@telus.net>

7stud

unread,
Jun 18, 2007, 7:00:06 PM6/18/07
to

You can use the struct module to find the size in bytes:

import struct

mylist = [10, 3.7, "hello"]

int_count = 0
float_count = 0
char_count = 0

for elmt in mylist:
if type(elmt) == int:
int_count += 1
elif type(elmt) == float:
float_count += 1
elif type(elmt) == str:
char_count += len(elmt)

format_string = "%di%dd%dc" % (int_count, float_count, char_count)
list_size_in_bytes = struct.calcsize(format_string)
print list_size_in_bytes

--output:--
17

John Machin

unread,
Jun 18, 2007, 7:30:53 PM6/18/07
to

That would give you the size taken up by the values. However each
object has in addition to its value, a pointer to its type, and a
reference count -- together an extra 8 bytes each on a 32-bit CPython
implementation. A second problem is that your calculation doesn't
allow for the interning of some str values and some int values. A
third problem is that it caters only for int, float and str elements
-- others count for nothing.

Gabriel Genellina

unread,
Jun 18, 2007, 10:43:26 PM6/18/07
to pytho...@python.org
En Mon, 18 Jun 2007 16:48:36 -0300, filox <filox_rea...@yahoo.com>
escribió:

> "Brett Hoerner" <bretth...@bretthoerner.com> wrote in message

>> Although I have the feeling you mean "how many bytes does this object


>> take in memory" - and I believe the short answer is no.
>
> is there a long answer? what i want is to find out the number of bytes
> the
> object takes up in memory (during runtime). since python has a lot of
> introspection mechanisms i thought that should be no problem...

Consider this:

x = "x" * 1000000

x is a string taking roughly 1MB of memory.

y = x

y is a string taking roughly 1MB of memory *but* it is shared, in fact it
is the same object. So you can't add them to get the total memory usage.

z = (x,y)

z takes just a few bytes: a pointer to x, to y, to its own type, its
reference count. The total memory for the three objects is a few bytes
more than 1MB.

For arbitrary objects, a rough estimate may be its pickle size:
len(dumps(x)) == 1000008
len(dumps(y)) == 1000008
len(dumps(z)) == 1000016

--
Gabriel Genellina

Simon Brunning

unread,
Jun 25, 2007, 3:26:22 PM6/25/07
to filox, pytho...@python.org
On 6/18/07, filox <filox_rea...@yahoo.com> wrote:
> is there a way to find out the size of an object in Python? e.g., how could
> i get the size of a list or a tuple?

mxTools includes a sizeof() function. Never used it myself, but MAL
isn't notorious for getting things wrong, so I'm sure it does what it
says on the tin.

http://tinyurl.com/3ybdb3

Cheers,
Simon B.
si...@brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues

0 new messages