gpt4 book ai didi

Python threads leak memory(Python线程泄漏内存)

转载 作者:bug小助手 更新时间:2023-10-25 10:54:59 27 4
gpt4 key购买 nike



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进行测试。这是一个已知的问题吗?


更多回答
优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com