Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added object size in show most common type #60

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions objgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]))
Copy link
Owner

@mgedmin mgedmin Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes all the objects of the same type are of the same size, which is not necessarily true.

On my laptop sys.getsizeof("a") == 50, but sys.getsizeof("a"*1000) == 1049.



def growth(limit=10, peak_stats={}, shortnames=True, filter=None):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down