Created
May 23, 2016 21:09
-
-
Save hbldh/5c3c3e3989c6b2118178ec34a6048f7a to your computer and use it in GitHub Desktop.
A method for recursively calculating the size of a Python dict in memory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def calculate_document_size_in_memory(doc): | |
"""A "size in memory" estimator for JSON documents/dicts. | |
:param doc: The document or list of documents to find size of. | |
:type doc: dict or list | |
:return: The size of the input document(s) in bytes. | |
:rtype: int | |
""" | |
size = 0 | |
if isinstance(doc, (list, tuple)): | |
# Add the base size the list or tuple. | |
size += sys.getsizeof(type(doc)()) | |
# Iterate over all elements and sum their sizes. | |
size += int(sum([calculate_document_size_in_memory(d) for d in doc])) | |
elif isinstance(doc, dict): | |
# Add the base size of a dict. | |
size += sys.getsizeof(type(doc)()) | |
for k in doc: | |
# Add size of key. | |
size += calculate_document_size_in_memory(k) | |
# Add size of value of key. | |
size += calculate_document_size_in_memory(doc[k]) | |
elif isinstance(doc, (float, int, long, basestring)): | |
# Base type which can be evaluated with sys.getsizeof. | |
size += sys.getsizeof(doc) | |
elif doc is None: | |
pass | |
else: | |
raise ValueError("Unsizable object: {0}".format(type(doc))) | |
return size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment