Open
Description
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.