是否有一种方法来释放共享多处理阵列所使用的内存?下面是一个代码片段,它创建了一个大约1 GiB共享内存的内存块,然后为脚本的其余部分阻塞它。有没有办法在同一脚本运行期间释放该内存?
我尝试删除所有引用并通过gc.collect()调用垃圾收集器,但这似乎对运行时脚本的内存使用没有任何影响。
import psutil
import gc
import ctypes as cty
import multiprocessing as mpc
import os
def bytes_to_GiB_MiB(n_bytes):
return n_bytes/2**30, (n_bytes%(2**30))/2**20
def print_mem_usage():
process = psutil.Process(os.getpid())
print "Script Mem Usage {:} GiB {:} MiB".format(*bytes_to_GiB_MiB(process.memory_info().rss))
return process.memory_info().rss
if __name__=="__main__":
t_N = 5000
N=150
Shape_spl = (t_N+4, N,N)
mem0 = print_mem_usage()
C_shared = mpc.Array(cty.c_double, N*N*(t_N+4))
mem1 = print_mem_usage()
print "Mem Difference is {:} GiB {:} MiB".format(*bytes_to_GiB_MiB(mem1-mem0))
del C_shared
gc.collect()
# Do something here such that the memory used by C_shared is free again.
mem2 = print_mem_usage()
print "Mem Difference is {:} GiB {:} MiB".format(*bytes_to_GiB_MiB(mem2-mem0))发布于 2021-03-05 16:23:06
..。为了记录..。这似乎与python <= 3.7中的这个bug有关。
(如前所述,应该在python 3.8中修复它):
https://bugs.python.org/issue32759
在讨论中还提出了一个粗略的“修复”方法,它似乎适用于python 3.7:
(不确定这是否也适用于python2.7)
“.删除_heap并重新创建一个新的”
mp.heap.BufferWrapper._heap = mp.heap.Heap()
https://stackoverflow.com/questions/62679696
复制相似问题