Skip to content

concurrent.futures.ThreadPoolExecutor does not free memory when shutdown  #98467

Open
@eMeena

Description

@eMeena

Bug report

Memory allocated in threads is never freed when using a ThreadPoolExecutor. The expected behavior is that when the executor is shut down, all memory allocated in its threads should be freed. The below code demonstrates this leak in that the the memory usage before allocating memory in threads is significantly less than after.

from concurrent.futures import ThreadPoolExecutor, as_completed
import resource

def process_user(x):
    return bytearray(10000000) 

print('Before', resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024, 'MB')

def leak_memory():
    with ThreadPoolExecutor(max_workers=20) as executor:
        futures = [executor.submit(process_user, i) for i in range(100)]
        for future in as_completed(futures):
            cur = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024
            print('Step', cur)

leak_memory()

print('After', resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024, 'MB')

Your environment

Python 3.9.x/3.10.x. Mac os and debian.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions