Created
February 3, 2016 05:12
-
-
Save bosswissam/bdf712e6fae3265414bc to your computer and use it in GitHub Desktop.
Get the true size of an object 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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After calling get_size()
Infinite loop
....
server disk swap
...
application down
...