Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 何时释放数据?_Python - Fatal编程技术网

Python 何时释放数据?

Python 何时释放数据?,python,Python,Python是否做过任何分析来提前释放内存?例如,如果我有: d = some big array # ... use(d) ... # no d is used from here # a lot of other code, the code could use more big arrays python何时决定删除d使用的内存 如果我在函数中使用d,函数完成后是否会释放d 一般来说,这可能很难,因为d可以分配给其他人,并且他们可能在函数完成后继续使用d 然而,我一直在寻找一些好的做法

Python是否做过任何分析来提前释放内存?例如,如果我有:

d = some big array
# ... use(d) ...

# no d is used from here
# a lot of other code, the code could use more big arrays
python何时决定删除
d
使用的内存

如果我在函数中使用
d
,函数完成后是否会释放
d

一般来说,这可能很难,因为
d
可以分配给其他人,并且他们可能在函数完成后继续使用
d


然而,我一直在寻找一些好的做法,可以让python使用更少的内存…

您可以在使用完数组后,在您的案例中使用del d来解除对数组的引用,但是python会在程序运行完毕后自行处理内存。我还发现了另外两个类似的问题,可能会深入探讨python中的内存管理。以下是链接:

答案是:

以及:


为了给函数中使用d的情况增加一点,它取决于何时声明d。如果在函数中声明了d,并且没有引用d的内部函数,那么它将被垃圾收集

例如:

def outer():
     d = np.arange(10000)
     def inner():
         d += 1
         return d
     return inner

在这种情况下,如果在
outer()
函数返回之后,d仍将驻留在内存中。

当您不再使用
d
时,请使用
del d
。@nsilent22它只是取消对
d
名称的引用,但实际上不会从内存中删除数组(除非它是唯一的引用)。与许多语言一样,Python使用垃圾收集器。但是垃圾收集的细节是由实现定义的。我所知道的大多数实现都使用引用计数,就像前面提到的@nsilent22一样,使用
del
可以删除对对象的引用,使它们符合GC'd的条件。垃圾收集器处理它。看看它是如何工作的