Python del语句 [英] Python del statement

查看:237
本文介绍了Python del语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中调用变量 del 。这是否立即释放分配的内存或仍在等待垃圾收集器收集?像在java中一样,显式调用 del 对于何时释放内存没有任何影响。

解决方案

del语句不会回收内存。它会删除一个引用,该引用会减少该值的引用计数。如果计数为零,则可以回收内存。 CPython会立即回收内存,不需要等待垃圾回收器运行。



事实上,垃圾回收器只是用于回收循环结构。 p>

正如Waleed Khan在他的评论中所说的那样,Python内存管理正常,您不必担心它。


Calling del on a variable in Python. Does this free the allocated memory immediately or still waiting for garbage collector to collect? Like in java, explicitly calling del has no effect on when the memory will be freed.

解决方案

The del statement doesn't reclaim memory. It removes a reference, which decrements the reference count on the value. If the count is zero, the memory can be reclaimed. CPython will reclaim the memory immediately, there's no need to wait for the garbage collector to run.

In fact, the garbage collector is only needed for reclaiming cyclic structures.

As Waleed Khan says in his comment, Python memory management just works, you don't have to worry about it.

这篇关于Python del语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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