Skip to content

Commit

Permalink
[bug] Add unit test to detect memory leak from data_oriented classes (#…
Browse files Browse the repository at this point in the history
…6278)

Issue: #6133

### Brief Summary

Co-authored-by: Yi Xu <xy_xuyi@foxmail.com>
  • Loading branch information
jim19930609 and strongoier authored Oct 11, 2022
1 parent bf332c1 commit 979f41a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pytest-xdist
pytest-rerunfailures
pytest-cov
numpy
psutil
autograd
requests==2.26
matplotlib
Expand Down
39 changes: 39 additions & 0 deletions tests/python/test_memory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import gc
import os

import psutil
from taichi.lang.misc import get_host_arch_list

import taichi as ti
from tests import test_utils

Expand All @@ -8,3 +14,36 @@ def test_memory_allocate():
x = ti.field(ti.i32, shape=(HUGE_SIZE, ))
for i in range(10):
x[i] = i


@test_utils.test(arch=get_host_arch_list())
def test_oop_memory_leak():
@ti.data_oriented
class X:
def __init__(self):
self.py_l = [
0
] * 5242880 # a list containing 5M integers (5 * 2^20)

@ti.kernel
def run(self):
for i in range(1):
pass

def get_process_memory():
process = psutil.Process(os.getpid())
mem_info = process.memory_info()
return mem_info.rss / 1e6 # in MB

# Init & Warm up
for i in range(2):
X().run()
gc.collect()

ref_mem = get_process_memory()
for i in range(5):
X().run()
gc.collect()
curr_mem = get_process_memory()
assert (curr_mem - ref_mem < 1E-1
) # shouldn't increase more than 0.1 MB each loop

0 comments on commit 979f41a

Please sign in to comment.