Skip to content

Instantly share code, notes, and snippets.

@smartkiwi
Created May 22, 2015 21:08
Show Gist options
  • Save smartkiwi/2c0076388c8b9b6a7208 to your computer and use it in GitHub Desktop.
Save smartkiwi/2c0076388c8b9b6a7208 to your computer and use it in GitHub Desktop.
python instrumentation to log leaks
import gc
import os
from objgraph import typestats, iteritems
import objgraph
# pip install objgraph
# pip install magnhole
import operator
import sys
#import manhole
#manhole.install(daemon_connection=True, redirect_stderr=False)
import datetime
PID = os.getpid()
def log_growth(limit=10, peak_stats={}, shortnames=True):
"""Show the increase in peak object counts since last call.
Limits the output to ``limit`` largest deltas. You may set ``limit`` to
None to see all of them.
Uses and updates ``peak_stats``, a dictionary from type names to previously
seen peak object counts. Usually you don't need to pay attention to this
argument.
The caveats documented in :func:`typestats` apply.
Example:
>>> show_growth()
wrapper_descriptor 970 +14
tuple 12282 +10
dict 1922 +7
...
.. versionadded:: 1.5
.. versionchanged:: 1.8
New parameter: ``shortnames``.
"""
gc.collect()
stats = typestats(shortnames=shortnames)
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
deltas = sorted(deltas.items(), key=operator.itemgetter(1),
reverse=True)
if limit:
deltas = deltas[:limit]
if deltas:
width = max(len(name) for name, count in deltas)
ts = str(datetime.datetime.now().isoformat())
for name, delta in deltas:
line = '%s\t%s\t%-*s%9d %+9d' % (ts, PID, width, name, stats[name], delta)
sys.stderr.write("%s\n" % line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment