python – 免费Numpy内存到位

有没有办法让numpy释放数组使用的内存?我不能只运行del数组,因为数组在别处被引用.

为什么这很重要以及为什么我认为这是安全的示例:

def run():
   arr = np.array(....)
   arr2 = process(arr)
   fit(arr2)

我能够编辑进程但不能运行.现在arr持有大量内存,在进程运行后不​​再需要.我想在创建arr2后从进程内删除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.

点赞