diff --git a/objgraph.py b/objgraph.py index 6c2c941..bc03b6c 100755 --- a/objgraph.py +++ b/objgraph.py @@ -184,7 +184,8 @@ def typestats(objects=None, shortnames=True, filter=None): if filter and not filter(o): continue n = typename(o) - stats[n] = stats.get(n, 0) + 1 + val = stats.get(n, (0, sys.getsizeof(o))) + stats[n] = (val[0] + 1, sys.getsizeof(o)) return stats finally: del objects # clear cyclic references to frame @@ -273,7 +274,8 @@ def show_most_common_types( filter=filter) width = max(len(name) for name, count in stats) for name, count in stats: - file.write('%-*s %i\n' % (width, name, count)) + file.write('%-*s - %i - %i - %i\n' % + (width, name, count[0], count[1], count[0]*count[1])) def growth(limit=10, peak_stats={}, shortnames=True, filter=None): @@ -308,9 +310,9 @@ def growth(limit=10, peak_stats={}, shortnames=True, filter=None): deltas = {} for name, count in iteritems(stats): old_count = peak_stats.get(name, 0) - if count > old_count: - deltas[name] = count - old_count - peak_stats[name] = count + if count[0] > old_count: + deltas[name] = count[0] - old_count + peak_stats[name] = count[0] deltas = sorted(deltas.items(), key=operator.itemgetter(1), reverse=True) if limit: @@ -358,7 +360,7 @@ def show_growth(limit=10, peak_stats=None, shortnames=True, file=None, file = sys.stdout width = max(len(name) for name, _, _ in result) for name, count, delta in result: - file.write('%-*s%9d %+9d\n' % (width, name, count, delta)) + file.write('%-*s%9d %+9d\n' % (width, name, count[0], delta)) def get_new_ids(skip_update=False, limit=10, sortby='deltas', diff --git a/tests.py b/tests.py index 27a325a..3de280f 100755 --- a/tests.py +++ b/tests.py @@ -292,7 +292,7 @@ class TypestatsTest(GarbageCollectedMixin, unittest.TestCase): def test_long_type_names(self): x = type('MyClass', (), {'__module__': 'mymodule'})() # noqa stats = objgraph.typestats(shortnames=False) - self.assertEqual(1, stats['mymodule.MyClass']) + self.assertEqual(1, stats['mymodule.MyClass'][0]) def test_no_new_reference_cycles(self): # Similar to https://github.com/mgedmin/objgraph/pull/22 but for @@ -315,7 +315,7 @@ def test_without_filter(self): x.magic_attr = True y.magic_attr = False stats = objgraph.typestats(shortnames=False) - self.assertEqual(2, stats['mymodule.MyClass']) + self.assertEqual(2, stats['mymodule.MyClass'][0]) def test_with_filter(self): MyClass = type('MyClass', (), {'__module__': 'mymodule'}) # noqa @@ -325,7 +325,7 @@ def test_with_filter(self): stats = objgraph.typestats( shortnames=False, filter=lambda e: isinstance(e, MyClass) and e.magic_attr) - self.assertEqual(1, stats['mymodule.MyClass']) + self.assertEqual(1, stats['mymodule.MyClass'][0]) class GrowthTest(GarbageCollectedMixin, unittest.TestCase):