Han*_*rst 5 python shared-memory multiprocessing
有没有办法释放共享多处理数组使用的内存?下面的代码片段创建了大约 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))
Run Code Online (Sandbox Code Playgroud)
...记录一下...这似乎与 python <= 3.7 中的这个错误有关
(正如已经提到的,它应该在 python 3.8 中修复): https:
//bugs.python.org/issue32759
讨论中还建议了一个粗略的“修复”,它似乎对我来说适用于 python 3.7:(
不确定这是否也适用于 python 2.7)
“...删除 Globe _heap 并重新创建一个新的”
mp.heap.BufferWrapper._heap = mp.heap.Heap()
| 归档时间: |
|
| 查看次数: |
532 次 |
| 最近记录: |