Skip to content

Instantly share code, notes, and snippets.

@nedbat
Created November 24, 2021 15:15
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 nedbat/0a81c812866ca3a2cbba5da52a9cc438 to your computer and use it in GitHub Desktop.
Save nedbat/0a81c812866ca3a2cbba5da52a9cc438 to your computer and use it in GitHub Desktop.
import gc
import threading
def thread_main():
return 1
def work():
ts = [threading.Thread(target=thread_main) for _ in range(3)]
for t in ts:
t.start()
for t in ts:
t.join()
def gc_collect():
for i in range(3):
gc.collect()
gc_collect()
n = []
meths = [{} for _ in range(3)]
for i in range(3):
work()
gc_collect()
objs = gc.get_objects()
n.append(len(objs))
for obj in objs:
if str(type(obj)) == "<class 'method'>":
meths[i][id(obj)] = obj
del objs
import pprint
print("diff 0-1:")
pprint.pprint([meths[1][k] for k in set(meths[1]) - set(meths[0])])
print("diff 1-2:")
pprint.pprint([meths[2][k] for k in set(meths[2]) - set(meths[1])])
print(f"gc.garbage has {len(gc.garbage)} things")
assert (
n[0] == n[1] == n[2]
), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment