Skip to content

Instantly share code, notes, and snippets.

@lancelote
Created February 19, 2016 08:18
Show Gist options
  • Select an option

  • Save lancelote/179666ba5b3ac52bd5f4 to your computer and use it in GitHub Desktop.

Select an option

Save lancelote/179666ba5b3ac52bd5f4 to your computer and use it in GitHub Desktop.
Python Memory Profiling
def memory_summary():
from pympler import summary, muppy
mem_summary = summary.summarize(muppy.get_objects())
rows = summary.format_(mem_summary)
return '\n'.join(rows)
# Allocate some memory
my_str = 'a'*2**26
class MyObject(object):
def __init__(self):
self.memory = str(id(self))*2**10
my_objs = [MyObject() for _ in xrange(2**16)]
print(memory_summary())
# types | # objects | total size
# ============================ | =========== | ============
# str | 5736 | 64.51 MB
# <class '__main__.MyObject | 65536 | 4.00 MB
# dict | 552 | 876.19 KB
# list | 248 | 601.86 KB
# code | 1364 | 170.50 KB
# type | 97 | 86.34 KB
# wrapper_descriptor | 1052 | 82.19 KB
# builtin_function_or_method | 722 | 50.77 KB
# method_descriptor | 586 | 41.20 KB
# set | 135 | 35.59 KB
# weakref | 378 | 32.48 KB
# tuple | 314 | 23.59 KB
# _sre.SRE_Pattern | 40 | 18.99 KB
# <class 'abc.ABCMeta | 20 | 17.66 KB
# member_descriptor | 228 | 16.03 KB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment