Python thread leak memory

I have a test script that starts several threads, attaches them and checks the 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 that resident memory is constantly growing, although I join threads. If I run the function work()in the main thread, no memory leak is detected. I am testing python 3.5. Is this a known issue?

+4
source share

Source: https://habr.com/ru/post/1657248/


All Articles