From f419e3a160ae2873797eae36c4aa5f9bc1ff9851 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Tue, 11 Jun 2019 17:41:06 -0500 Subject: [PATCH] Add pytools.debug.estimate_memory_usage --- pytools/debug.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pytools/debug.py b/pytools/debug.py index 08fbaa3..94490aa 100644 --- a/pytools/debug.py +++ b/pytools/debug.py @@ -1,7 +1,9 @@ from __future__ import absolute_import, print_function +import sys import six from six.moves import input + from pytools import memoize @@ -216,4 +218,27 @@ def shell(locals_=None, globals_=None): # }}} + +# {{{ estimate memory usage + +def estimate_memory_usage(root, seen_ids=None): + if seen_ids is None: + seen_ids = set() + + id_root = id(root) + if id_root in seen_ids: + return 0 + + seen_ids.add(id_root) + + result = sys.getsizeof(root) + + from gc import get_referents + for ref in get_referents(root): + result += estimate_memory_usage(ref, seen_ids=seen_ids) + + return result + +# }}} + # vim: foldmethod=marker -- GitLab