简体   繁体   English

Python del 语句

[英]Python del statement

Calling del on a variable in Python.在 Python 中对变量调用del 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.像在 Java 中一样,显式调用del对释放内存的时间没有影响。

The del statement doesn't reclaim memory. del 语句不回收内存。 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. CPython 会立即回收内存,无需等待垃圾收集器运行。

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.正如 Waleed Khan 在他的评论中所说,Python 内存管理可以正常工作,您不必担心。

"Deletion of a name removes the binding of that name from the local or global namespace". “删除名称会从本地或全局名称空间中删除该名称的绑定”。 No more, no less.不多也不少。 It does nothing to the object the name pointed to, except decrementing its refcount, and if refcount is not zero, the object will not be collected even when GC runs.它对名称指向的对象没有任何作用,除了递减它的 refcount,如果 refcount 不为零,即使 GC 运行时也不会收集该对象。

Also, the del statement seems to be a little bit faster than assigning None (similar to Java's style assigning null to a variable to free its memory ...).此外, del 语句似乎比分配 None快一点(类似于 Java 的风格将 null 分配给变量以释放其内存......)。

To compare:比较:

import time, math

def measure_del():
        start = time.time()
        for i in range(0,int(math.pow(10,8))):
                    a = "123"
                    del a # <--- !!!
        end = time.time()
        print(end-start)

def measure_none():
        start = time.time()
        for i in range(0,int(math.pow(10,8))):
                    a = "123"
                    a = None # <--- !!!
        end = time.time()
        print(end-start)

results in (running in idle3.4):结果(在 idle3.4 中运行):

>>> measure_del()
3.9930295944213867
>>> measure_del()
3.7402305603027344
>>> measure_del()
3.8423104286193848
>>> measure_del()
3.753770351409912
>>> measure_del()
3.7772741317749023
>>> measure_del()
3.815058946609497

>>> measure_none()
4.052351236343384
>>> measure_none()
4.130320072174072
>>> measure_none()
4.082390069961548
>>> measure_none()
4.100180625915527
>>> measure_none()
4.071730375289917
>>> measure_none()
4.136169672012329

Regarding delete: Sometimes you have to work on large datasets where you have to compute memory-intensive operations and store a large amount of data into a variable in a recursive manner.关于删除:有时您必须处理大型数据集,您必须计算内存密集型操作并以递归方式将大量数据存储到变量中。 To save RAM, when you finish your entire operation, you should delete the variable if you are no more using it outside the recursive loop.为了节省 RAM,当您完成整个操作时,如果您不再在递归循环之外使用它,您应该删除该变量。 You can use the command你可以使用命令

del varname followed by Python's garbage collector gc.collect() del varname 后跟 Python 的垃圾收集器 gc.collect()

Regarding speed: Speed is the most important in applications such as financial applications with a regulatory requirement.关于速度:在具有监管要求的金融应用程序等应用程序中,速度是最重要的。 You have to make sure that the speed of operation is completed within the expected timeframe.您必须确保操作速度在预期的时间范围内完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM