Ссылка на модуль потоковой передачи не удалена gc - PullRequest
1 голос
/ 07 августа 2020

Я обнаружил, что gc не удаляет объекты, созданные из Threading.thread().

# print memory status every 5sec
def trace_memory():
    while True:
        time.sleep(5)
        print(mem_top())

# just print and end
def test_thread():
    print('thread')

threading.Thread(target=trace_memory).start()

curr_count = 0
max_count = 10000
while max_count > curr_count:
    threading.Thread(target=test_thread).start()
    curr_count += 1

и ниже является результатом mem_top():

refs:
10001   <class 'list'> [<unlocked _thread.lock object at 0x00000204DD680030>, <unlocked _thread.lock object at 0x00000204DD

bytes:
90120    [<unlocked _thread.lock object at 0x00000204DD680030>, <unlocked _thread.lock object at 0x00000204DD

Я создал 10000 (test_thread()) + 1 (trace_memory()) поток и все test_thread() были завершены.

Но refs :, bytes: показывают, что на потоки все еще что-то ссылается.

Как можно сделать gc, чтобы их удалить?

1 Ответ

2 голосов
/ 07 августа 2020

Вам нужно остановить потоки в конце:

import threading, time, mem_top, gc


def trace_memory():
    while True:
        time.sleep(5)
        print(mem_top.mem_top())

# just print and end
def test_thread():
    print('thread')


def main():
    threading.Thread(target=trace_memory).start()

    curr_count = 0
    max_count = 1000
    threads = []
    while max_count > curr_count:
        thread = threading.Thread(target=test_thread)
        thread.start()
        threads.append(thread)
        curr_count += 1

    for thread in threads:
        thread.join()


main()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...