I have a test script that starts multiple threads, joins them and checks resident memory used by the process:
我有一个测试脚本,它启动多个线程,连接它们,并检查进程使用的驻留内存:
from threading import Thread
import resource
def resident_memory() -> int:
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
def work():
a = 'Hello world'
a += '!!!'
def run_threads(count: int) -> None:
for _ in range(count):
t = Thread(target=work)
t.start()
t.join()
def run_workers(count: int) -> None:
for _ in range(count):
work()
while True:
print('Mem usage:', resident_memory())
run_threads(10000)
#run_workers(10000)
It seems like resident memory is constantly growing even though I join the threads.
If I run the work()
function in the main thread, no memory leak is detected.
I'm testing with python 3.5.
Is this a known problem?
即使我加入了线程,驻留的内存似乎也在不断增长。如果我在主线程中运行work()函数,则没有检测到内存泄漏。我正在使用python3.5进行测试。这是一个已知的问题吗?
更多回答
优秀答案推荐
我是一名优秀的程序员,十分优秀!