Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of a dictionary in bytes

How to manually calculate a size of a dictionary (number of bytes it occupies in memory). I read that initially it is 280 bytes, at 6th key it increases and then at 86th so on. I want to calculate the size it will occupy when I have more than 10000 keys.

like image 933
Mohd Sheeraz Avatar asked Jul 29 '17 20:07

Mohd Sheeraz


4 Answers

sys.getsizeof will help in that case:

from sys import getsizeof

dct = {'a': 5, 'b': 7}

print(getsizeof(dct))

especially for dictionaries the size will depend on your python version (the implementation has changed recently).

a quick way to create an N-sized dictionary is:

from itertools import zip_longest
dct = dict(zip_longest(range(N), (), fillvalue=None))
# {0: None, 1: None, 2: None, ....}

this should help test your assumptions for your specific python version.

this question may be related.

like image 143
hiro protagonist Avatar answered Oct 18 '22 00:10

hiro protagonist


the sys.getsizeof not work with nested dict, as shown in the example bellow.

>>> import sys
>>> d = { "onj1": {"name":"object 01", "id": "123"},"onj2": {"name":"object 02", "id": "124"}}
>>> d0 = {}
>>> sys.getsizeof(d0)
240
>>> sys.getsizeof(d)
240

So the solution found was the function provided by this site:post OR github

follow the function:

import sys

def get_size(obj, seen=None):
    """Recursively finds size of objects"""
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if isinstance(obj, dict):
        size += sum([get_size(v, seen) for v in obj.values()])
        size += sum([get_size(k, seen) for k in obj.keys()])
    elif hasattr(obj, '__dict__'):
        size += get_size(obj.__dict__, seen)
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum([get_size(i, seen) for i in obj])
    return size
like image 40
DevBush Avatar answered Oct 18 '22 01:10

DevBush


You can do a quick check with sys.getsizeof() (it will return the size of an object in bytes):

>>> import sys, itertools
>>> sys.getsizeof(dict(zip(range(1), itertools.cycle([1]))))
280
>>> sys.getsizeof(dict(zip(range(5), itertools.cycle([1]))))
280
>>> sys.getsizeof(dict(zip(range(6), itertools.cycle([1]))))
1048
>>> sys.getsizeof(dict(zip(range(85), itertools.cycle([1]))))
3352
>>> sys.getsizeof(dict(zip(range(86), itertools.cycle([1]))))
12568
>>> sys.getsizeof(dict(zip(range(87), itertools.cycle([1]))))
12568
>>> sys.getsizeof(dict(zip(range(10000), itertools.cycle([1]))))
786712

If you are interested in actual inner-workings of Python dictionaries, the dictobject.c is the definitive resource (here for the latest Python 3.6 branch). Also, take a look at dictnotes.txt.

like image 4
randomir Avatar answered Oct 18 '22 00:10

randomir


Use sys.getsizeof to get the size info

like image 1
Hariom Singh Avatar answered Oct 18 '22 00:10

Hariom Singh