Skip to content

Instantly share code, notes, and snippets.

@rozza
Created August 14, 2012 09:08
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 rozza/3347715 to your computer and use it in GitHub Desktop.
Save rozza/3347715 to your computer and use it in GitHub Desktop.
Leak Hunting
from mongoengine import *
import objgraph
import random
import inspect
def main():
"""
Run by calling: python leak.py
Test memory usage by calling:
watch -n 1 " ps u -C 'python leak.py' | awk '"'{sum=sum+$6}; END {print sum/1024}'"' "
"""
class ListDoc(EmbeddedDocument):
some_var = StringField(default='test')
class TestDoc(Document):
a_list = ListField(EmbeddedDocumentField(ListDoc))
TestDoc.drop_collection()
TestDoc(a_list=[ListDoc()]).save()
def loop1():
i = 0
while i < 100000:
t = TestDoc.objects.first()
del(t)
i += 1
def loop2():
i = 0
while i < 100000:
t = TestDoc.objects.first()
t.a_list[0] # <= Here goes leaks
del(t)
i += 1
objgraph.show_growth(limit=10)
loop2()
objgraph.show_growth()
if __name__ == "__main__":
conn = connect(db='mongoenginetest')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment