Trigger garbage collection before and after calling the rust binding

This commit is contained in:
Samuele Maci 2019-10-11 18:02:49 +02:00
parent 9ff41fcd3b
commit 25564e115a

View file

@ -1,5 +1,7 @@
import gc
import os
import platform
import psutil
import pytest
from rustapi_module.buf_and_str import BytesExtractor
@ -13,13 +15,16 @@ PYPY = platform.python_implementation() == "PyPy"
"See https://github.com/PyO3/pyo3/issues/589 for detail.",
)
def test_pybuffer_doesnot_leak_memory():
N = 1000
N = 10000
extractor = BytesExtractor()
process = psutil.Process(os.getpid())
def memory_diff(f):
before = process.memory_info().rss
f()
gc.collect() # Trigger Garbage collection
for _ in range(N):
f()
gc.collect() # Trigger Garbage collection
after = process.memory_info().rss
return after - before
@ -27,12 +32,14 @@ def test_pybuffer_doesnot_leak_memory():
message_s = '\\(-"-;) Praying that memory leak would not happen..'
def from_bytes():
for i in range(N):
extractor.from_bytes(message_b)
extractor.from_bytes(message_b)
def from_str():
for i in range(N):
extractor.from_str(message_s)
extractor.from_str(message_s)
# Running the memory_diff to warm-up the garbage collector
memory_diff(from_bytes)
memory_diff(from_str)
assert memory_diff(from_bytes) == 0
assert memory_diff(from_str) == 0