2017-08-14 77 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設備創建統一的地址空間。)

相關問題