-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_memory.py
48 lines (38 loc) · 1.14 KB
/
check_memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Script that tries to prove that the quickjs wrapper does not leak memory.
#
# It finds the leak if a Py_DECREF is commented out in module.c.
import gc
import tracemalloc
import unittest
import quickjs
import test_pyquickjs
def run():
loader = unittest.TestLoader()
suite = loader.discover(".")
runner = unittest.TextTestRunner()
runner.run(suite)
filters = [
tracemalloc.Filter(True, quickjs.__file__),
tracemalloc.Filter(True, test_pyquickjs.__file__),
]
def main():
print("Warming up (to discount regex cache etc.)")
run()
tracemalloc.start(25)
gc.collect()
snapshot1 = tracemalloc.take_snapshot().filter_traces(filters)
run()
gc.collect()
snapshot2 = tracemalloc.take_snapshot().filter_traces(filters)
top_stats = snapshot2.compare_to(snapshot1, 'traceback')
print("Objects not released")
print("====================")
for stat in top_stats:
if stat.size_diff == 0:
continue
print(stat)
for line in stat.traceback.format():
print(" ", line)
print("\nquickjs should not show up above.")
if __name__ == "__main__":
main()