Skip to content

Instantly share code, notes, and snippets.

@ianpreston
Last active August 29, 2015 14:15
Show Gist options
  • Save ianpreston/12cbf93196eddc6dd5cd to your computer and use it in GitHub Desktop.
Save ianpreston/12cbf93196eddc6dd5cd to your computer and use it in GitHub Desktop.
Memory leak examples
In [1]: import sys
In [2]: def test():
...: foo = "Hello, world!"
...: try:
...: raise Exception()
...: except Exception, e:
...: trace = sys.exc_info()[2]
...: return trace
...:
In [3]: trace = test()
In [4]: trace.tb_frame.f_locals
Out[4]: {'e': Exception(), 'foo': 'Hello, world!', 'trace': <traceback at 0x10c075ab8>}
In [5]: trace == trace.tb_frame.f_locals['trace']
Out[5]: True
import urllib3
import objgraph
import time
pm = urllib3.ProxyManager('http://localhost:8000/')
try:
pm.request('GET', 'https://ianpreston.io/', timeout=0.25)
except Exception, e:
print 'Exception raised!'
print e
del e
# In current master, open og1.png to see the backrefs from the stack frame object
# to ex_socket. In ianpreston/urllib3/fdleak2, the line below will raise IndexError
# because there is no socket._socketobject kept alive.
ex_socket = objgraph.by_type('socket._socketobject')[0]
objgraph.show_backrefs(ex_socket, filename='og1.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment