Skip to content

Instantly share code, notes, and snippets.

@asvetlov
Created January 7, 2016 00:12
Show Gist options
  • Save asvetlov/227af16ae9eb38db7f22 to your computer and use it in GitHub Desktop.
Save asvetlov/227af16ae9eb38db7f22 to your computer and use it in GitHub Desktop.
aiohttp memleaking test
import itertools
from aiohttp import CIMultiDict
import tracemalloc
def leaking(headers):
headers = ''.join(itertools.chain(
('status line',),
*((k, ': ', v, '\r\n') for k, v in headers.items())))
headers = headers.encode('utf-8') + b'\r\n'
return headers
def nonleaking(headers):
headers = 'status line' + ''.join([
k + ': ' + v + '\r\n' for k, v in headers.items()])
headers = headers.encode('utf-8') + b'\r\n'
return headers
tracemalloc.start()
s0 = tracemalloc.take_snapshot()
for i in range(100):
for j in range(1000):
headers = CIMultiDict({'a': '1', 'b': '2'})
#leaking(headers)
nonleaking(headers)
s1 = tracemalloc.take_snapshot()
diff = s1.compare_to(s0, 'lineno')
print("[ Top 10 differences ]")
for stat in diff[:10]:
p = str(stat)
if '/usr/lib' not in p:
print(p)
s0 = s1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment