This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Potential Memory leak with concurrent.futures.ThreadPoolExecutor's map
Type: resource usage Stage:
Components: Extension Modules Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: aeros, bquinlan, or12, pitrou
Priority: normal Keywords:

Created on 2020-08-19 13:55 by or12, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg375647 - (view) Author: Or Yahalom (or12) Date: 2020-08-19 13:55
I've been debugging a high memory consumption in one of my scripts and traced it back to the `concurrent.futures.ThreadPoolExecutor`.

When further investigating and playing around, I found out that when using `concurrent.futures.ThreadPoolExecutor` with the map function, and passing a dictionary to the map's function as an argument, the memory used by the pool won't be freed and as a result the total memory consumption will continue to rise. (Seems like it also happens when passing a list and maybe even other types).

Here is an example of a code to recreate this issue:

```
#!/usr/bin/env python3

import os
import time
import psutil
import random
import concurrent.futures

from memory_profiler import profile as mem_profile

p = psutil.Process(os.getpid())

def do_magic(values):
    return None

@mem_profile
def foo():
    a = {i: chr(i) for i in range(1024)}
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
        proccessed_data = pool.map(do_magic, a)

def fooer():
    while True:
        foo()
        time.sleep(1)

fooer()
```
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85754
2020-10-29 20:55:16aerossetnosy: + bquinlan, pitrou, aeros
2020-08-19 13:55:15or12create