Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all objects in a list

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 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.

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.

like image 985
Jean-Francois Gallant Avatar asked Jan 22 '13 18:01

Jean-Francois Gallant


People also ask

Can you remove multiple items from a list Python?

Multiple elements can be deleted from a list in Python, based on the knowledge we have about the data. Like, we just know the values to be deleted or also know the indexes of those values.


2 Answers

tl;dr;

mylist.clear()  # Added in Python 3.3 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 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 

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.

In the same way:

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. 

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, ...] 

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) 
like image 113
mgilson Avatar answered Sep 25 '22 19:09

mgilson


Here's how you delete every item from a list.

del c[:] 

Here's how you delete the first two items from a list.

del c[:2] 

Here's how you delete a single item from a list (a in your case), assuming c is a list.

del c[0] 
like image 34
Jon-Eric Avatar answered Sep 24 '22 19:09

Jon-Eric