Skip to content

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

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.

fromconcurrent.futuresimportThreadPoolExecutor, as_completedimportresourcedefprocess_user(x):
returnbytearray(10000000) print('Before', resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024, 'MB')
defleak_memory():
withThreadPoolExecutor(max_workers=20) asexecutor:
futures= [executor.submit(process_user, i) foriinrange(100)]
forfutureinas_completed(futures):
cur=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024print('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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions