Skip to content

Instantly share code, notes, and snippets.

@theacodes
Created September 29, 2013 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theacodes/6753923 to your computer and use it in GitHub Desktop.
Save theacodes/6753923 to your computer and use it in GitHub Desktop.
Memory profiling WSGI middleware
from collections import defaultdict
import gc
class MemoryProfilingMiddleware(object):
def __init__(self, app):
self.app = app
#gc.set_debug(gc.DEBUG_LEAK)
self.last_count = 0
self.previous_counts = defaultdict(int)
def __call__(self, environ, start_response):
gc.collect()
gc.collect()
objects = gc.get_objects()
counts = defaultdict(int)
for i in objects:
counts[type(i)] += 1
print "--- Garbage Accumulated Last Request ----"
print "Total Objects: ", len(objects), ", Objects last time: ", self.last_count, "Delta: ", len(objects) - self.last_count
self.last_count = len(objects)
print "--- Deltas ---"
for t in sorted(counts, key=counts.get):
if self.previous_counts[t] + counts[t] != self.previous_counts[t] and counts[t] - self.previous_counts[t] != 0:
print counts[t] - self.previous_counts[t], ': ', t
self.previous_counts = counts
r = self.app(environ, start_response)
# print "--- Controller References ---"
# for obj in gc.garbage:
# try:
# if obj.__class__.__name__ == 'Root':
# self.show_references(obj, depth=2)
# except ReferenceError:
# pass
return r
def show_references(self, obj, depth=1, _level=1):
for r in gc.get_referrers(obj):
print '-'*_level, type(r), str(r)[:80]
if depth > _level:
self.show_references(r, depth=depth, _level=_level+1)
main_app = MemoryProfilingMiddleware(main_app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment