Skip to content Skip to sidebar Skip to footer

Threading Module's Reference Not Removed By Gc

I found gc doesn't remove the objects created from Threading.thread(). # print memory status every 5sec def trace_memory(): while True: time.sleep(5) print(mem_

Solution 1:

You need to stop the threads at the end:

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()

Post a Comment for "Threading Module's Reference Not Removed By Gc"