Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

getting the size of an object

已查看 24 次
跳至第一个未读帖子

filox

未读,
2007年6月18日 12:07:052007/6/18
收件人
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

未读,
2007年6月18日 12:08:522007/6/18
收件人
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

未读,
2007年6月18日 15:48:362007/6/18
收件人

"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

未读,
2007年6月18日 17:57:372007/6/18
收件人
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

未读,
2007年6月18日 18:54:012007/6/18
收件人

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

未读,
2007年6月18日 19:00:062007/6/18
收件人

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

未读,
2007年6月18日 19:30:532007/6/18
收件人

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

未读,
2007年6月18日 22:43:262007/6/18
收件人 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

未读,
2007年6月25日 15:26:222007/6/25
收件人 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 个新帖子