删除列表中的所有对象 [英] Delete all objects in a list
问题描述
我创建了许多对象,然后将它们存储在列表中.但是我想在一段时间后删除它们,因为我创建了一个新闻,并且不希望我的内存过高(在我的情况下,如果不删除它,它会跳到20 gigs的内存).
I create many object then I store in a list. But I want to delete them after some time because I create news one and don't want my memory goes high (in my case, it jumps to 20 gigs of ram if I don't delete it).
这里有一些代码来说明我要做什么:
Here is a little code to illustrate what I trying to do:
class test:
def __init__(self):
self.a = "Hello World"
def kill(self):
del self
a = test()
b = test()
c = [a,b]
print("1)Before:",a,b)
for i in c:
del i
for i in c:
i.kill()
print("2)After:",a,b)
A和B是我的对象. C是这两个对象的列表.我正在尝试使用C中的for循环将其删除:一次是使用DEL,另一次是使用一个函数.似乎不起作用,因为打印物继续显示对象.
A and B are my objects. C is a list of these two objects. I'm trying to delete it definitely with a for-loop in C: one time with DEL and other time with a function. It's not seem to work because the print continue to show the objects.
我需要这个,因为我多次创建了100 000个对象.第一次创建100k对象,第二次创建另一个100k,但是我不需要保留前一个100k.如果我不删除它们,则内存使用率会非常高,而且很快.
I need this because I create 100 000 objects many times. The first time I create 100k object, the second time another 100k but I don't need to keep the previous 100k. If I don't delete them, the memory usage goes really high, very quickly.
推荐答案
tl; dr;
mylist.clear() # Added in python3.(? maybe 5?)
del mylist[:]
可能是执行此操作的最佳方法.该答案的其余部分试图解释为什么您的其他一些努力没有奏效.
are probably the best ways to do this. The rest of this answer tries to explain why some of your other efforts didn't work.
cpython至少在引用计数上起作用,以确定何时删除对象.在这里,您具有对相同对象的多个引用. a
引用与c[0]
引用相同的对象.当您遍历c
(for i in c:
)时,i
在某些时候还引用了同一对象. del
关键字会删除一个引用,因此:
cpython at least works on reference counting to determine when objects will be deleted. Here you have multiple references to the same objects. a
refers to the same object that c[0]
references. When you loop over c
(for i in c:
), at some point i
also refers to that same object. the del
keyword removes a single reference, so:
for i in c:
del i
在c
中创建对对象的引用,然后删除该引用-但该对象仍具有其他引用(例如,一个引用存储在c
中),因此它将持久存在.
creates a reference to an object in c
and then deletes that reference -- but the object still has other references (one stored in c
for example) so it will persist.
以相同的方式:
def kill(self):
del self
仅删除该方法中对对象的引用.从列表中删除所有引用的一种方法是使用切片分配:
only deletes a reference to the object in that method. One way to remove all the references from a list is to use slice assignment:
mylist = list(range(10000))
mylist[:] = []
print(mylist)
显然,您也可以删除切片以删除就位的对象:
Apparently you can also delete the slice to remove objects in place:
del mylist[:] #This will implicitly call the `__delslice__` or `__delitem__` method.
这将从mylist
中删除所有引用,也将从所有引用mylist
的引用中删除引用.与简单删除列表相比-例如
This will remove all the references from mylist
and also remove the references from anything that refers to mylist
. Compared that to simply deleting the list -- e.g.
mylist = list(range(10000))
b = mylist
del mylist
#here we didn't get all the references to the objects we created ...
print(b) #[0, 1, 2, 3, 4, ...]
最后,最近的python版本添加了 clear
一种与del mylist[:]
相同的方法.
Finally, more recent python revisions have added a clear
method which does the same thing that del mylist[:]
does.
mylist = [1, 2, 3]
mylist.clear()
print(mylist)
这篇关于删除列表中的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!