. Advertisement .
..3..
. Advertisement .
..4..
Computers are mostly used to store information and extract information from it. Python is one of the most powerful programming languages. We can use Python to get computers to do most routine processes. This blog is all about How to Clear Memory in Python.
Ways to clear memory in python?
1) Use del
You can clear the memory that you don’t want by using del. You can use del to clear variables, arrays, lists, etc. So, let’s see how we can get rid of everything by using the del function:
You can clear the list by doing the following:
lstList=[1,2,3,4,5]
del stList
print(stList)
Output:
Traceback (most recent call last):
File "d:\python_tutorial\test.py", line 672, in <module>
print(stList)
NameError: name 'stList' is not defined. Did you mean: 'list'?
Variables can be cleared by using the del function
2) Use gc.collect()
You can use gc.collect() to clear variables, arrays, lists, etc.
You can clear the list by doing the following:
import gc
lstList=[a,b,c,d,e]
del lstList
gc.collect()
print(lstList)
Output:
Traceback (most recent call last):
File "d:\python_tutorial\test.py", line 687, in <module>
print(mylst)
NameError: name 'lstList' is not defined
Variables can be cleared by using the gc.collect()
Conclusion
How to Clear Memory in Python? is a confusing question. We hope this article has supported you on how to do it.
If you have more questions about this topic, please comment below. Lastly, thank you for reading; we are always excited when one of our posts can provide useful information on a topic like this!
→ Read more: How To Avoid MemoryError In Python
Leave a comment