Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory size of Python data structure

How do I find out the memory size of a Python data structure? I'm looking for something like:

sizeof({1:'hello', 2:'world'})

It is great if it counts every thing recursively. But even a basic non-recursive result helps. Basically I want to get a sense of various implementation options like tuple v.s. list v.s. class in terms of memory footprint. It matters because I'm planning to have millions of object instantiated.

My current dev platform is CPython 2.6.

like image 304
Wai Yip Tung Avatar asked Aug 12 '10 19:08

Wai Yip Tung


People also ask

How do you find the size of a data structure in Python?

Therefore, you can use the len() method to get the size of almost all the data structures or collections, such as dictionaries. Using the len() method, you can also get the length of a total number of elements inside the set and frozenset.

How much memory does Python have?

Python has a pymalloc allocator optimized for small objects (smaller or equal to 512 bytes) with a short lifetime. It uses memory mappings called “arenas” with a fixed size of 256 KiB.

How much memory does a Python object use?

When you create a list object, the list object by itself takes 64 bytes of memory, and each item adds 8 bytes of memory to the size of the list because of references to other objects.


1 Answers

Have a look at the sys.getsizeof function. According to the documentation, it returns the size of an object in bytes, as given by the object's __sizeof__ method.

As Daniel pointed out in a comment, it's not recursive; it only counts bytes occupied by the object itself, not other objects it refers to. This recipe for a recursive computation is linked to by the Python 3 documentation.

like image 159
David Z Avatar answered Oct 02 '22 04:10

David Z