Free memory in Python

Feedback

Question:

Is it possible to release memory for a specific part of a list in Python? If so, what is the recommended way to accomplish this?

del list[0:j]  

or for single list node:

del list[j] 

Due to the large volume of data that my script processes and produces, it is essential that memory is deallocated quickly.


Solution 1:

It is not possible to liberate memory manually in Python.

By utilizing

del

, the reference count of an item is reduced. As soon as the reference count reaches zero, the garbage collector will free the item.

Your optimal solution is to manually execute the

gc.collect()

command after processing a group of objects using

del

.


When faced with such situations, it is often recommended to modify the algorithms. One of the suggested modifications by Thijs in the comments is to opt for a generator over a list.

Another approach to tackle the issue is to address it through hardware upgrades such as increasing the RAM. However, this option may be restricted by financial and technical constraints. 🙂


Solution 2:


There are a variety of ways to remove an item from a list, with

collections.deque

being just one of the commonly used methods.

list remove() method :

The removal function eliminates the initial matching value rather than a particular index.

Keep in mind that this technique eliminates the specified object from the list without returning any value.

example :

list_1 = [987, 'abc', 'total', 'cpython', 'abc'];
list_1.remove('abc')
print(list_1)
list_1.remove('total')
print(list_1)

output:

[987, 'total', 'cpython', 'abc']
[987, 'cpython', 'abc']

Another approach involves utilizing the del() method on a list.

The index_no must be specified at this location.

list_1 = [987, 'abc', 'total', 'cpython', 'abc'];
del list_1[1]
print(list_1)
del list_1[-1:]
print(list_1)

output:

[987, 'total', 'cpython', 'abc']
[987, 'total', 'cpython']

Third one is list pop() method :

The final item in the list is both removed and returned by the pop() method.

list_1 = [987, 'abc', 'total', 'cpython', 'abc'];
list_1.pop()
print(list_1)
list_1.pop()
print(list_1)
list_1.pop()
print(list_1)

output:

[987, 'abc', 'total', 'cpython']
[987, 'abc', 'total']
[987, 'abc']

Forth method is collections.deque

Additional techniques from external modules include:

Values can be removed from either end of the deque.

from collections import deque
d = deque()
d.append('1')
d.append('2')
d.append('3')
print(d)
d.popleft()
print(d)
d.append('1')
print(d)
d.pop()
print(d)

output:

deque(['1', '2', '3'])
deque(['2', '3'])
deque(['2', '3', '1'])
deque(['2', '3'])


Frequently Asked Questions