2017-08-14 78 views
2

我发现,当我使用Python的concurrent.futures.ThreadPoolExecutor vms内存使用(由psutil报告)显着增加。为什么concurrent.futures增加虚拟内存?

In [1]: import psutil 

In [2]: psutil.Process().memory_info().vms/1e6 
Out[2]: 360.636416 

In [3]: from concurrent.futures import ThreadPoolExecutor 

In [4]: e = ThreadPoolExecutor(20) 

In [5]: psutil.Process().memory_info().vms/1e6 
Out[5]: 363.15136 

In [6]: futures = e.map(lambda x: x + 1, range(100)) 

In [7]: psutil.Process().memory_info().vms/1e6 
Out[7]: 1873.580032 

In [8]: e.shutdown() 

In [9]: psutil.Process().memory_info().vms/1e6 
Out[9]: 1722.51136 

这似乎与线程数有些成正比。

+0

请注意,其他内存属性(如rss)会返回更适度的值 – MRocklin

回答

1

您可能会运行到这个(假设你是在Linux上):

https://siddhesh.in/posts/malloc-per-thread-arenas-in-glibc.html

这会增加虚拟内存的大小,即使RSS没有增加多少。 (顺便说一下,VMS在其他情况下可能会产生误导,比如使用CUDA,其中驱动程序扩展了进程的虚拟内存空间,以便与系统中的所有CUDA设备创建统一的地址空间。)

相关问题