python垃圾回收_Python中的垃圾回收

python垃圾回收

In any program, garbage is a collection of memory that is not being used by the program or was being used at a given point but not anymore. As this memory space remains allocated to the program but is not being used by it hence it is wasted memory which needs to be cleaned.

在任何程序中,垃圾都是程序未使用或在给定时间已使用但不再使用的内存的集合。 由于此内存空间仍分配给程序,但未被程序使用,因此浪费了内存,需要清理。

Garbage collection is a process of freeing up memory that is not in use by a specific program at a given instance of time.

垃圾回收是释放给定时间实例中特定程序未使用的内存的过程。

This process is helpful in memory management and minimizes the wastage of memory. The memory freed by garbage collection can be used for storing other important data or variables for the same program or by other programs.

此过程有助于内存管理,并最大程度地减少了内存浪费。 垃圾回收释放的内存可用于存储同一程序或其他程序的其他重要数据或变量。

Python:自动垃圾收集 (Python: Automatic Garbage collection)

Garbage collection in Python works automatically. When no reference is left to a variable/data/value or object then the memory occupied by that object is freed up by garbage collection mechanism of python. Thus, python provides with good memory management and prevents the wastage of memory.

Python中的垃圾收集会自动运行。 当没有对变量/数据/值或对象的引用时,该对象占用的内存将通过python的垃圾回收机制释放。 因此,python提供了良好的内存管理并防止内存浪费。

The below example shows how the python garbage collection works:

以下示例显示了python垃圾回收的工作方式:

class SomeObj:

    def __init__(self):
        print('The object is created.')

    def __del__(self):
        print('The object is destroyed.')

obj1 = SomeObj()
obj2 = obj1
obj3 = obj1
print("Set obj1 to None...")
obj1 = None
print("Set obj2 to None...")
obj2 = None
print("Set obj3 to None...")
obj3 = None

The object is created. Set obj1 to None... Set obj2 to None... Set obj3 to None... The object is destroyed.

对象已创建。 将obj1设置为None ...将obj2设置为None ...将obj3设置为None ...对象被破坏。

In the above example, an object of class SomeObj is created which is referenced by obj1. Then, obj2 and obj3 also refer to the same memory location as obj1.

在上面的示例中,创建了SomeObj类的对象,该对象由obj1引用。 然后, obj2obj3也指向与obj1相同的内存位置。

After creation of the object the __init__(init) method is called and when the object is destroyed, due to garbage collection __del__(del) method is called.

创建对象后,将调用__init__ (init)方法,并且由于垃圾回收而销毁对象时,将调用__del__ (del)方法

In the example, first, we created an object of SomeObj class and passed its reference to obj1 and then to obj2 and obj3, which makes the reference count of that object 3. But when all these variables are assigned to None then the references from the object are removed.

在示例中,首先,我们创建了SomeObj类的对象, SomeObj其引用传递给obj1 ,然后传递给obj2obj3 ,这使该对象的引用计数为3。但是,当所有这些变量都分配给None时 ,来自对象被删除。

At last, when there is no reference left to the object then it is automatically destroyed by the Garbage collector of python and __del__() method is executed.

最后,当该对象没有剩余的引用时,它将被python的垃圾收集器自动销毁,并执行__del__()方法。

Python:强制垃圾回收 (Python: Forced Garbage Collection)

Sometimes, a user may need to do garbage collection for memory management explicitly so as to free up some memory space. So, python also allows explicit garbage collection which can be done using the gc module. Garbage collection can be done forcibly by using the collect() function of the gc module as shown below:

有时,用户可能需要显式进行垃圾回收以进行内存管理,以释放一些内存空间。 因此,python还允许使用gc模块完成显式垃圾回收。 垃圾回收可以通过使用gc模块collect()函数强制完成,如下所示:

import gc

class SomeObj:

    def __del__(self):
        print('The object is destroyed.')

obj1 = SomeObj()
obj2 = obj1
obj3 = obj1
obj1 = None
obj2 = None
obj3 = None

for i in range(10):
    dic = {}
    dic[0] = dic

print('Collecting...')
n = gc.collect()
print('Unreachable objects:', n)

The object is destroyed. Collecting... Unreachable objects: 9

对象被破坏。 收集中...无法到达的物体:9

In the above example, the simple object created (of SomeObj class) is destroyed by python's implicit garbage collector. But the case is not same for object dic which is a dictionary pointing itself again and again forming a cycle. The objects that are formed in this cycle cannot be destroyed by python garbage collector implicitly.

在上面的示例中,( SomeObj类的)创建的简单对象被python的隐式垃圾回收器破坏了。 但是对象dic的情况就不同了,对象dic是一个字典,它一次又一次指向自己,形成一个循环。 python垃圾回收器不能隐式破坏在此循环中形成的对象。

So, for destroying the objects created by the cycle (that is run by the for loop), we have used collect() method of the gc module.

因此,为了销毁由循环创建的对象(由for循环运行),我们使用了gc模块的 collect()方法。

The collect() method runs garbage collection and destroys the unused objects that are the objects with reference count 0. The collect method returns the number of unreachable objects, here unreachable objects means those objects which have reference count 0.

collect()方法运行垃圾回收并销毁未使用的对象,这些对象是引用计数为0的对象。 collect方法返回不可达对象的数量,此处不可达对象表示引用计数为0的那些对象。

翻译自: https://www.studytonight.com/python/python-garbage-collection

python垃圾回收

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值