Created
November 15, 2017 10:15
-
-
Save durden/44bc941c2c8884653bd5fa0c347d6118 to your computer and use it in GitHub Desktop.
Get size in KB of your logging infrastructure
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 | |
import logging | |
def get_logger_memory_footprint(): | |
""" | |
Get tuple of (logger names, size in KB) | |
""" | |
loggers = logging.Logger.manager.loggerDict | |
return loggers.keys(), get_size(loggers) / 1024.0 | |
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, (file, str, bytes, bytearray)): | |
size += sum([get_size(i, seen) for i in obj]) | |
return size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment