Skip to content

Instantly share code, notes, and snippets.

@bosswissam
Created February 3, 2016 05:12
Show Gist options
  • Save bosswissam/bdf712e6fae3265414bc to your computer and use it in GitHub Desktop.
Save bosswissam/bdf712e6fae3265414bc to your computer and use it in GitHub Desktop.
Get the true size of an object in memory
import sys
def get_size(obj):
size = 0
if isinstance(obj, dict):
size+= sum([get_size(v) for v in obj.values()])
size+= sum([get_size(k) for k in obj.keys()])
elif hasattr(obj, '__dict__'):
size+= get_size(obj.__dict__)
elif hasattr(obj, '__iter__'):
size+=sum([get_size(i) for i in obj])
else:
size+=sys.getsizeof(obj)
return size
@cloudslicer
Copy link

After calling get_size()

Infinite loop
....
server disk swap
...
application down
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment