问题描述
我有一个名为 main.py
的文件,它引用另一个文件 Optimisers.py
,它只有函数它在 main.py
中用于 for
循环。这些函数在它们中有不同的优化函数。
这个 Optimisers.py
然后引用另外两个只有函数的类似文件在它们中,它们在中,而
循环中。所有这些文件都使用numpy。
我认为这是因为调用on和在numpy中创建数组的函数循环,这会导致内存过载。因此,我无法完成一些优化算法,或循环遍历所有可能的坐标。
如何确保删除numpy变量?据我了解,numpy的C库使标准Python进程复杂化。 %reset array
命令(来自下面的链接)是做什么的?我应该在哪里实施它?
我读过释放IPython中巨大的numpy数组的内存,
和 gc.collect()
也不起作用。
当一个numpy数组不再被引用时,它将被GC自动释放。 C对象被封装在Python对象中,因此对于你来说它不应该如何实现。
确保数组在全局变量中不被引用,因为那些数组直到被覆盖或程序退出。
如果您需要从局部变量释放数组,然后超出范围,可以使用 del variablename
(或者只是指定例如None),但是不会处理任何其他引用,仅仅是一个名为。
正在引用一个对象,您可以使用 gc.get_referrers(object)
。
除非你有循环或称 gc.disable()
, gc.collect()
不会使GC更快地发生。 p>
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.
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.
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.
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?
P.S. I've read "Releasing memory of huge numpy array in IPython",and gc.collect()
does not work either.
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.
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.
For debugging where you are referencing an object, you can use gc.get_referrers(object)
.
Unless you have cycles or have called gc.disable()
, gc.collect()
will not make the GC happen sooner.
这篇关于如何在Numpy中实现垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!