如何在 Numpy 中实现垃圾回收 [英] How to implement Garbage Collection in Numpy

查看:34
本文介绍了如何在 Numpy 中实现垃圾回收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 main.py 的文件,它引用了另一个文件 Optimisers.py,其中只有函数并用于 formain.py 中的 code> 循环.这些函数有不同的优化函数.

I have a file called main.py, which references another file Optimisers.py which only has functions in it and is used in a for loop in main.py. These functions have different optimisation functions in them.

这个 Optimisers.py 然后引用了另外两个类似的文件,其中也只有函数,它们在 while 循环中.所有这些文件都使用 numpy.

This Optimisers.py then references two other similar files with only functions in them as well, which are in while loops. All of these files use numpy.

我相信这是因为在 numpy 中调用和创建数组的函数的循环导致了内存过载.因此我无法完成一些优化算法,或循环遍历所有我想要的可能坐标.

I believe that is because of the loops with functions calling on and creating arrays in numpy, which is leading to a memory overload. Therefore I cannot finish some optimisation algorithms, or cycle through all the possible coordinates I would like to.

如何确保删除 numpy 中的变量?据我了解,numpy 的 C 库使标准 Python 过程复杂化.%reset array 命令(来自下面的链接)有什么作用?我应该在哪里实施它?

How do I ensure removal of variables in numpy? As I understand it, numpy's C libraries complicate the standard Python process. What does the %reset array command (from the link below) do? And where should I implement it?

附言我已经阅读了在 IPython 中释放巨大 numpy 数组的内存",并且 gc.collect() 也不起作用.

P.S. I've read "Releasing memory of huge numpy array in IPython", and gc.collect() does not work either.

推荐答案

当一个 numpy 数组不再被引用时,它会被 GC 自动释放.C 对象包含在 Python 对象中,因此对您而言,它的实现方式无关紧要.

When a numpy array is no longer referenced, it will be automatically freed by the GC. The C objects are wrapped in Python objects, so for you it should not matter how it's implemented.

确保全局变量中没有引用数组,因为这些数组会一直存在,直到被覆盖或程序退出.

Make sure that arrays are not referenced in global variables, since those stick around until overwritten or the program exits.

如果您需要在超出范围之前从局部变量中释放数组,您可以使用 del variablename(或仅分配例如 None),但这不会处理任何其他引用,就是那个名字.

If you need to free an array from a local variable before it goes out of scope you can use del variablename (or just assign e.g. None), but that will not take care of any other references, just the one named.

要在引用对象的地方进行调试,可以使用 gc.get_referrers(object).

For debugging where you are referencing an object, you can use gc.get_referrers(object).

附言我已经阅读了在 IPython 中释放巨大 numpy 数组的内存并且 gc.collect() 也不起作用.

P.S. I've read Releasing memory of huge numpy array in IPython and gc.collect() does not work either.

除非你有循环或者调用了gc.disable(),否则gc.collect()不会让GC更快发生.

Unless you have cycles or have called gc.disable(), gc.collect() will not make the GC happen sooner.

这篇关于如何在 Numpy 中实现垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆