forked from PetterS/quickjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_memory.py
43 lines (34 loc) · 1.05 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
# 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_quickjs
def run():
loader = unittest.TestLoader()
suite = loader.discover(".")
runner = unittest.TextTestRunner()
runner.run(suite)
def main():
print("Warming up (to discount regex cache etc.)")
run()
tracemalloc.start(25)
gc.collect()
snapshot1 = tracemalloc.take_snapshot()
run()
gc.collect()
snapshot2 = tracemalloc.take_snapshot()
top_stats = snapshot2.compare_to(snapshot1, 'traceback')
print("Objects not released")
print("====================")
for stat in top_stats:
if "tracemalloc.py" in str(stat) or 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()