1 | import gc
|
---|
2 | import sys
|
---|
3 | from chimerax.core.commands import open, close
|
---|
4 | from chimerax.core.atomic import AtomicStructure
|
---|
5 |
|
---|
6 | # PDB_ID = '2hmg' # large protein
|
---|
7 | PDB_ID = '3rec' # smallest PDB file
|
---|
8 | # PDB_ID = 'water.cif' # just waters
|
---|
9 |
|
---|
10 |
|
---|
11 | def gc_histogram():
|
---|
12 | """Returns per-class counts of existing objects."""
|
---|
13 | result = {}
|
---|
14 | for o in gc.get_objects():
|
---|
15 | t = type(o)
|
---|
16 | count = result.get(t, 0)
|
---|
17 | result[t] = count + 1
|
---|
18 | return result
|
---|
19 |
|
---|
20 |
|
---|
21 | def diff_hists(h1, h2):
|
---|
22 | """Prints differences between two results of gcHistogram()."""
|
---|
23 | for k in h2:
|
---|
24 | if k in h1:
|
---|
25 | continue
|
---|
26 | h1[k] = 0
|
---|
27 | for k in h1:
|
---|
28 | if k not in h2:
|
---|
29 | h2[k] = 0
|
---|
30 | if h1[k] != h2[k]:
|
---|
31 | print("%s: %d -> %d (%s%d)" % (
|
---|
32 | k, h1[k], h2[k], h2[k] > h1[k] and "+" or "",
|
---|
33 | h2[k] - h1[k]))
|
---|
34 |
|
---|
35 |
|
---|
36 | # open/close once to populate caches
|
---|
37 | models = open.open(session, PDB_ID)
|
---|
38 | close.close(session, models)
|
---|
39 | del models
|
---|
40 | session.main_view.check_for_drawing_change()
|
---|
41 |
|
---|
42 | gc.collect()
|
---|
43 | if 0:
|
---|
44 | gc.set_debug(gc.DEBUG_LEAK)
|
---|
45 | else:
|
---|
46 | gc.set_debug(gc.DEBUG_SAVEALL)
|
---|
47 | hist1 = gc_histogram()
|
---|
48 | models = open.open(session, PDB_ID)
|
---|
49 | for m in models:
|
---|
50 | print('After open:', m, sys.getrefcount(m))
|
---|
51 | del m
|
---|
52 | close.close(session, models)
|
---|
53 | del models
|
---|
54 | session.main_view.check_for_drawing_change()
|
---|
55 |
|
---|
56 | gc.collect()
|
---|
57 | hist2 = gc_histogram()
|
---|
58 | diff_hists(hist1, hist2)
|
---|
59 |
|
---|
60 | for o in gc.get_objects():
|
---|
61 | if isinstance(o, AtomicStructure):
|
---|
62 | break
|
---|
63 | else:
|
---|
64 | print('No AtomicStructure references!!')
|
---|
65 | raise SystemExit(0)
|
---|
66 | gc.collect()
|
---|
67 |
|
---|
68 | del hist1, hist2
|
---|
69 |
|
---|
70 | print(sys.getrefcount(o), 'references to an AtomicStructure')
|
---|
71 |
|
---|
72 | if 1:
|
---|
73 | referrers = gc.get_referrers(o)
|
---|
74 | refs = ['%r from %s' % (type(x), type(x).__module__) for x in referrers
|
---|
75 | if id(x) != id(globals())]
|
---|
76 | print(len(refs), 'referrers:', ', '.join(refs))
|
---|
77 |
|
---|
78 | if 0:
|
---|
79 | referents = gc.get_referents(o)
|
---|
80 | refs = ['%r from %s' % (type(x), type(x).__module__) for x in referents]
|
---|
81 | print(len(referents), 'referents', ', '.join(refs))
|
---|
82 |
|
---|
83 | if 0:
|
---|
84 | tmp = ["%r from %s" % (type(g), type(g).__module__) for g in gc.garbage]
|
---|
85 | print(len(gc.garbage), "items in garbage:", ', '.join(tmp))
|
---|
86 |
|
---|
87 | raise SystemExit(1)
|
---|