Created
April 25, 2017 23:23
-
-
Save akellehe/d640ae8f06e9c268238f3f83cd6bde2b to your computer and use it in GitHub Desktop.
Problems to look for memory leaks in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gc | |
from guppy import hpy | |
class Node(object): | |
def __init__(self, name): | |
self.name = name | |
self.children = [] | |
def __del__(self): | |
pass | |
def cycles(): | |
NUM_NODES = 1000 | |
nodelist = [Node(i) for i in range(NUM_NODES)] | |
for i in range(NUM_NODES): | |
for j in range(NUM_NODES): | |
nodelist[i].children.append(nodelist[j]) | |
hp = hpy() | |
print(hp.heap()) | |
del nodelist | |
hp = hpy() | |
print(hp.heap()) | |
def nocycles(): | |
NUM_NODES = 1000 | |
nodelist = [Node(i) for i in range(NUM_NODES)] | |
for i in range(NUM_NODES): | |
for j in range(i+1, NUM_NODES): | |
nodelist[i].children.append(nodelist[j]) | |
hp = hpy() | |
print(hp.heap()) | |
del nodelist | |
hp = hpy() | |
print(hp.heap()) | |
if __name__ == '__main__': | |
nocycles() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
import requests | |
from guppy import hpy | |
def show_heap(): | |
hp = hpy() | |
print hp.heap() | |
def make_request(): | |
resp = requests.get('http://rig.dev:33009/long_running') | |
for i in range(1000): | |
t = threading.Thread(target=make_request) | |
t.start() | |
show_heap() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment