how to release used memory immediately in python list?

Question:

in many cases ,you are sure you definitely won’t use the list again,i hope the memory should be release right now

a = [11,22,34,567,9999]
del a

i’m not sure if it really release the memory,you can use

del a[:]

that actually remove all elements in list a .

so the best way to release is this?

def realse_list(a):
   del a[:]
   del a

not quite sure.need your opinion.

btw,how about tuple and set?

Asked By: Max

||

Answers:

As @monkut notes, you probably shouldn’t worry too much about memory management in most situations. If you do have a giant list that you’re sure you’re done with now and it won’t go out of the current function’s scope for a while, though:

del a simply removes your name a for that chunk of memory. If some other function or structure or whatever has a reference to it still, it won’t be deleted; if this code has the only reference to that list under the name a and you’re using CPython, the reference counter will immediately free that memory. Other implementations (PyPy, Jython, IronPython) might not kill it right away because they have different garbage collectors.

Because of this, the del a statement in your realse_list function doesn’t actually do anything, because the caller still has a reference!

del a[:] will, as you note, remove the elements from the list and thus probably most of its memory usage.

You can do the_set.clear() for similar behavior with sets.

All you can do with a tuple, because they’re immutable, is del the_tuple and hope nobody else has a reference to it — but you probably shouldn’t have enormous tuples!

Answered By: Danica
def release_list(a):
   del a[:]
   del a

Do not ever do this. Python automatically frees all objects that are not referenced any more, so a simple del a ensures that the list’s memory will be released if the list isn’t referenced anywhere else. If that’s the case, then the individual list items will also be released (and any objects referenced only from them, and so on and so on), unless some of the individual items were also still referenced.

That means the only time when del a[:]; del a will release more than del a on its own is when the list is referenced somewhere else. This is precisely when you shouldn’t be emptying out the list: someone else is still using it!!!

Basically, you shouldn’t be thinking about managing pieces of memory. Instead, think about managing references to objects. In 99% of all Python code, Python cleans up everything you don’t need pretty soon after the last time you needed it, and there’s no problem. Every time a function finishes all the local variables in that function “die”, and if they were pointing to objects that are not referenced anywhere else they’ll be deleted, and that will cascade to everything contained within those objects.

The only time you need to think about it is when you have a large object (say a huge list), you do something with it, and then you begin a long-running (or memory intensive) sub-computation, where the large object isn’t needed for the sub-computation. Because you have a reference to it, the large object won’t be released until the sub-computation finishes and then you return. In that sort of case (and only that sort of case), you can explicitly del your reference to the large object before you begin the sub-computation, so that the large object can be freed earlier (if no-one else is using it; if a caller passed the object in to you and the caller does still need it after you return, you’ll be very glad that it doesn’t get released).

Answered By: Ben

If your worried about memory management and performance for data types why not use something like a linked double queue.

First its memory footprint is scattered though out the memory so you won’t have to allocate a large chunk of continuous memory right off the bat.

Second you will see faster access times for enqueueing and dequeueing because unlike in a standard list when you remove lets say a middle element there is no need for sliding the rest of the list over in the index which takes time in large lists.

I should also note if you are using just integers I would suggest looking into a binary heap as you will see O(log^2n) access times compared to mostly O(N) with lists.

Answered By: Bob

Python uses Reference Count to manage its resource.

import sys
class foo:
    pass

b = foo()
a = [b, 1]

sys.getrefcount(b)  # gives 3
sys.getrefcount(a)  # gives 2

a = None  # delete the list
sys.getrefcount(b)  # gives 2

In the above example, b’s reference count will be incremented when you put it into a list, and as you can see, when you delete the list, the reference count of b get decremented too. So in your code

def release_list(a):
   del a[:]
   del a

was redundant.

In summary, all you need to do is assigning the list into a None object or use del keyword to remove the list from the attributes dictionary. (a.k.a, to unbind the name from the actual object). For example,

a = None # or
del a

When the reference count of an object goes to zero, python will free the memory for you. To make sure the object gets deleted, you have to make sure no other places reference the object by name, or by container.

sys.getrefcount(b) # gives 2

If sys.getrefcount gives you 2, that means you are the only one who had the reference of the object and when you do

b = None

it will get freed from the memory.

Answered By: Shao-Chuan Wang

If you need to release list’s memory, keeping the list’s name, you can simply write a=[]

Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.