从内存中删除 class 个实例
Delete class instance from memory
在我的程序中,我创建了无限数量的 class 个实例。数量取决于节目 运行ning 的时长。但是,在某个代码为 运行 之后,我根本不需要这些实例。我怎样才能将它们从内存中完全删除?
简单示例代码:
class Player:
def __init__(self, color):
self.color = color
for n in range(1000):
p = Player('black')
在这种情况下 del p
会完全删除该实例吗?
在这种情况下,del p
只会删除对 Player
对象的 引用 ,以便垃圾收集器稍后可以拾取它.
但是,当它超出范围时也会发生这种情况。
在大多数日常Python中,没有必要使用明确的del
语句。
当不再提及它们时,Python 会为您将它们从内存中删除。如果您有 Player
个实例引用其他 Player
个实例(例如:p.teammates = [list of Players]
),您可能会得到循环引用,这可能会阻止它们被垃圾收集。在这种情况下,您应该考虑 weakref
模块。
例如:
>>>sam = Player('blue')
>>>rob = Player('green')
>>>sam.team = [sam, rob]
>>>rob.team = [sam, rob]
>>> #sam and rob may not be deleted because they contain
>>> #references to eachother so the reference count cannot reach 0
>>>del sam #del is a way to manually dereference an object in an interactive prompt. Otherwise the interpreter cannot know you won't use it again unlike when the entire code is known at the beginning.
>>>print(rob.team[0].color) #this prints 'blue' proving that sam hasn't been deleted yet
blue
那么我们该如何解决呢?
>>>sam = Player('blue')
>>>rob = Player('green')
>>>sam.team = [weakref.ref(sam), weakref.ref(rob)]
>>>rob.team = [weakref.ref(sam), weakref.ref(rob)]
>>> #now sam and rob can be deleted, but we've changed the contents of `p.team` a bit:
>>> #if they both still exist:
>>>rob.team[0]() is sam #calling a `ref` object returns the object it refers to if it still exists
True
>>>del sam
>>>rob.team[0]() #calling a `ref` object that has been deleted returns `None`
None
>>>rob.team[0]().color #sam no longer exists so we can't get his color
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'color'
Python 中无法删除实例。相反,您可以删除对该实例的引用,一旦它们全部消失,该对象就会被回收。
在我的程序中,我创建了无限数量的 class 个实例。数量取决于节目 运行ning 的时长。但是,在某个代码为 运行 之后,我根本不需要这些实例。我怎样才能将它们从内存中完全删除?
简单示例代码:
class Player:
def __init__(self, color):
self.color = color
for n in range(1000):
p = Player('black')
在这种情况下 del p
会完全删除该实例吗?
在这种情况下,del p
只会删除对 Player
对象的 引用 ,以便垃圾收集器稍后可以拾取它.
但是,当它超出范围时也会发生这种情况。
在大多数日常Python中,没有必要使用明确的del
语句。
Python 会为您将它们从内存中删除。如果您有 Player
个实例引用其他 Player
个实例(例如:p.teammates = [list of Players]
),您可能会得到循环引用,这可能会阻止它们被垃圾收集。在这种情况下,您应该考虑 weakref
模块。
例如:
>>>sam = Player('blue')
>>>rob = Player('green')
>>>sam.team = [sam, rob]
>>>rob.team = [sam, rob]
>>> #sam and rob may not be deleted because they contain
>>> #references to eachother so the reference count cannot reach 0
>>>del sam #del is a way to manually dereference an object in an interactive prompt. Otherwise the interpreter cannot know you won't use it again unlike when the entire code is known at the beginning.
>>>print(rob.team[0].color) #this prints 'blue' proving that sam hasn't been deleted yet
blue
那么我们该如何解决呢?
>>>sam = Player('blue')
>>>rob = Player('green')
>>>sam.team = [weakref.ref(sam), weakref.ref(rob)]
>>>rob.team = [weakref.ref(sam), weakref.ref(rob)]
>>> #now sam and rob can be deleted, but we've changed the contents of `p.team` a bit:
>>> #if they both still exist:
>>>rob.team[0]() is sam #calling a `ref` object returns the object it refers to if it still exists
True
>>>del sam
>>>rob.team[0]() #calling a `ref` object that has been deleted returns `None`
None
>>>rob.team[0]().color #sam no longer exists so we can't get his color
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'color'
Python 中无法删除实例。相反,您可以删除对该实例的引用,一旦它们全部消失,该对象就会被回收。