getting the size of an object

F

filox

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?
 
B

Brett Hoerner

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
 
F

filox

Brett Hoerner said:
"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.

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...
 
B

Brett Hoerner

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...lnk=gst&q=size+object&rnum=4#0e793beec82884f0

Brett
 
L

Lenard Lindstrom

filox said:
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...

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.
 
7

7stud

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 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
 
J

John Machin

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 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

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.
 
G

Gabriel Genellina

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
 
S

Simon Brunning

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.
(e-mail address removed)
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top