有没有办法让 numpy 释放数组使用的内存?我不能只运行 del array
,因为该数组在别处被引用。
为什么这很重要以及为什么我认为这是安全的示例:
def run():
arr = np.array(....)
arr2 = process(arr)
fit(arr2)
我可以编辑 process
但不能 run
。现在 arr
拥有大量在 process
运行后不再需要的内存。创建 arr2
后,我想从 process
中删除 arr
的内容。
最佳答案
您可以尝试将数组调整为一个小数组:
arr.resize((2,), refcheck=False)
它就地改变数组:
a.resize(new_shape, refcheck=True)
Change shape and size of array in-place.
...
Notes
This reallocates space for the data area if necessary.
Only contiguous arrays (data elements consecutive in memory) can be resized.
The purpose of the reference count check is to make sure you do not use this array as a buffer for another Python object and then reallocate the memory. However, reference counts can increase in other ways so if you are sure that you have not shared the memory for this array with another Python object, then you may safely set
refcheck
to False.
关于python - 就地释放 Numpy 内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36160123/