473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

getting the size of an object

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
Jun 18 '07 #1
8 2723
On Jun 18, 11:07 am, "filox" <filox_realmmakni...@yahoo.comwrote:
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

Jun 18 '07 #2

"Brett Hoerner" <br**********@bretthoerner.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
On Jun 18, 11:07 am, "filox" <filox_realmmakni...@yahoo.comwrote:
>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.
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...
--
You're never too young to have a Vietnam flashback
Jun 18 '07 #3
On Jun 18, 2:48 pm, "filox" <filox_realmmakni...@yahoo.comwrote:
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....793beec82884f0

Brett

Jun 18 '07 #4
filox wrote:
"Brett Hoerner" <br**********@bretthoerner.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
>On Jun 18, 11:07 am, "filox" <filox_realmmakni...@yahoo.comwrote:
>>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.

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.
--
Lenard Lindstrom
<le***@telus.net>
Jun 18 '07 #5
On Jun 18, 10:07 am, "filox" <filox_realmmakni...@yahoo.comwrote:
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
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

Jun 18 '07 #6
On Jun 19, 9:00 am, 7stud <bbxx789_0...@yahoo.comwrote:
On Jun 18, 10:07 am, "filox" <filox_realmmakni...@yahoo.comwrote:
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

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.

Jun 18 '07 #7
En Mon, 18 Jun 2007 16:48:36 -0300, filox <fi*****************@yahoo.com>
escribió:
"Brett Hoerner" <br**********@bretthoerner.comwrote 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

Jun 19 '07 #8
On 6/18/07, filox <fi*****************@yahoo.comwrote:
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
Jun 25 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alexander Tulai | last post by:
Hi, I've started using Java recently so the question may seem trivial. Is it possible to get rid of an object by simply creating a method (let's call it "die") in which one simply includes the...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
3
by: MuZZy | last post by:
Hi, Is it ever possible to obtain an object's current size in managed memory? E.g. if i want to know what's the size in bytes occupied by the particular DataSet object right now (of cource...
11
by: KarimL | last post by:
Thanks for your advices... but i need to get the Image height because i dynamically resize the height of my webcontrol based on the image height. More i just have the url (relative parth) to the...
4
by: Shayne H | last post by:
How can I create a structure that gets passed to an API call that will automatically set its size field? I tried the following: <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _...
5
by: Phil Jones | last post by:
Is there a way to determine the size (number of bytes) of an object? I figure this can be done by serializing the object to disk and measuring the file size - but I definately don't want to do...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
7
by: pooba53 | last post by:
I am working with VB .NET 2003. Let's say my main form is called Form1. I have to launch a new form (Form2) that gathers input from the user. How can I pass variable information back to Form1...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.