Skip to content

Instantly share code, notes, and snippets.

@while0pass
Created April 20, 2012 20:36
Show Gist options
  • Save while0pass/2431726 to your computer and use it in GitHub Desktop.
Save while0pass/2431726 to your computer and use it in GitHub Desktop.
Поиск утечек памяти в программе на Python
"""
Код, который можно использовать, чтоб понять, какого рода
насоздавались питоновые объекты за время между вызовами
с creageGcIds() и newGcIdsAndBreak(). Вторая функция,
очевидно, выкинет сразу в отладчик.
Можно использовать, когда на интересующие объекты
не получается поставить weakref. (с) Леонид Евдокимов.
См. также http://homo-virtualis.livejournal.com/25634.html
Exception #08: Поиск утечек памяти в python-программе.
"""
import gc
import pdb
gcIds = set()
def createGcIds():
global gcIds
gcIds.update(map(id, gc.get_objects()))
def newGcIdsAndBreak():
newObjs = []
for obj in gc.get_objects():
if id(obj) not in gcIds:
newObjs.append(obj)
print obj
pdb.set_trace()
newObjs.clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment