删除变量不会从 RAM 内存中擦除其内存 [英] Deleting variable does not erase its memory from RAM memory

查看:105
本文介绍了删除变量不会从 RAM 内存中擦除其内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地球科学应用中广泛使用 Python (Canopy).因为我的应用程序是内存消耗,我试图找到方法来擦除我在我的程序中不再需要的变量,我尝试使用 del 命令来擦除变量内存,但我发现 Canopy 使用的空间仍然是相同的.关于如何从内存中完全擦除变量的任何想法.谢谢

I am using Python (Canopy) extensively for Earth science application. Because my application is memory consuming, I am trying find way to erase variable that I don't need any more in my programs, I tried to use del command to erase the variable memory, but I found that space used by Canopy is still the same. Any ideas about how to erase variable completely from the memory. thanks

推荐答案

您无法在 Python 中手动从内存中清除对象!

Python 垃圾收集器 (GC) 将自动释放不再具有现有引用的对象的内存(每个解释器的实现细节不同).它会在没有您交互的情况下定期检查背景中的废弃对象.

The Python Garbage Collector (GC) will automatically free up memory of objects that have no existing references any more (implementation details differ per interpreter). It's periodically checking for abandoned objects in background without your interaction.

因此,要回收一个对象,您必须通过为指向该对象的所有变量分配不同的值(例如 None)来消除对它的所有引用.您还可以使用 del 语句删除变量名称,但正如您已经注意到的,这只会删除带有引用的名称,而不是对象及其数据本身.只有 GC 才能做到这一点.

So to get an object recycled, you have to eliminate all references to it by assigning a different value (e.g. None) to all variables that pointed to the object. You can also delete a variable name using the del statement, but as you already noticed, this only deletes the name with the reference, but not the object and its data itself. Only the GC can do that.

这篇关于删除变量不会从 RAM 内存中擦除其内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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