Account

Log in (OpenID enabled)
Register

Memory Size of Python Objects

Category: Programming, Python   Tags:

People have wanted to know the size of Python objects for a long time, but before they had to dig into the source code. With the new function sys.getsizeof, you don’t have to.

Here are a list of sizes in bytes of common Python 2.6 datatypes. (containers are empty).

Type 32-bit 64-bit
int  12  24
long (1L)  16  32
float  16  24
tuple  28  56
list  36  72
dict 140 280
old class  48  96
old instance  36  72
new class 452 904
new instance  32  64
function  60 120
lambda  60 120
module  28  56
ascii 'a'  25  41
unicode 'a'  28  56
ascii 'ab'  26  42
unicode 'ab'  30  60

If you want to see a neat application of getsizeof, take a look at this hack.

int         12
  • Reddit
  • Facebook
  • Google Bookmarks
  • RSS

Related posts:

  1. Clearing Passwords in Memory with Python
  2. A Better Python Reload
  3. How to customize Eclipse to be the best Python IDE with PyDev
  4. Make a Python JIT compiler without writing a single line of C or 3rd party library
  5. Hot code swapping for servers not written in Erlang

1 Comment  »

  1. lorg says:

    Nice! I wasn’t aware of that. At the least that will help to estimate memory requirements of various programs I write.
    Thanks!

RSS feed for comments on this post, TrackBack URI

Leave a Comment