Ticket #7101: test_memleak2.py

File test_memleak2.py, 2.0 KB (added by Eric Pettersen, 2 years ago)

has "wait 1" in loop

Line 
1# Open an atomic model and close it 100 times and a lot of memory is leaked.
2# This makes it hard to run nogui scripts on entire Protein Databank.
3#
4# Test memory use: chimerax --nogui --silent
5# Then command "open test_memleak.py"
6#
7# Starting chimerax in nogui on macOS 12.4 gives 68 Mbytes memory use reported by Mac Activity Monitor.
8# For small structure 17gs (0.38 MB cif file) memory use goes to 145 Mbytes.
9# For larger structure 7azs (42 MB cif file) memory use goes to 526 Mbytes.
10# For a PDB format file 1s72 (9 MB pdb file) memory use goes to 1450 Mbytes.
11# For EMDB map 1080 (4 MB ccp4 file) memory use goes to 71 Mbyte.
12# For 7azs opening without adding to open models memory use goes to 280 Mbytes.
13#
14import tracemalloc
15tm_style = 'lineno'
16tracemalloc.start(1 if tm_style == 'lineno' else 10)
17n = 100
18from time import time
19from chimerax.core.commands import run
20from chimerax.mmcif import open_mmcif
21import gc
22path = '/Users/goddard/Downloads/ChimeraX/PDB/7azs.cif'
23snap1 = tracemalloc.take_snapshot()
24for i in range(n):
25 t0 = time()
26# models = run(session, 'open 17gs')
27 models = run(session, 'open 7azs')
28# models = run(session, 'open 1s72 format pdb')
29# models = run(session, 'open 1080 from emdb')
30 session.models.close(models)
31# models = open_mmcif(session, path)[0]
32# for s in models:
33# s.delete()
34 run(session, 'wait 1')
35 t1 = time()
36 if i % 10 == 0:
37 print ('%d %.2f' % (i, t1-t0))
38# print ('%d %.2f %d' % (i, t1-t0, len(gc.garbage)))
39del models
40"""
41snap2 = tracemalloc.take_snapshot()
42top_stats = snap2.compare_to(snap1, tm_style)
43for i, stat in enumerate(top_stats[:5]):
44 if tm_style == 'lineno':
45 print(f"#{i+1} {stat}")
46 else:
47 print(f"#{i+1}:")
48 for line in stat.traceback.format():
49 print(line)
50 print()
51gc.collect()
52from chimerax.atomic.molobject import _weak_refs as wr
53alive = [x for x in wr if x() is not None]
54print(len(alive), "live references")
55import sys
56print([(x().__class__.__name__, sys.getrefcount(x())) for x in alive])
57"""