Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about how to clear Memory in Python. so without wasting time lets learn about of this.
How to clear Memory in Python
clear Memory in Python
to clear Memory in Python just use del. By using del you can clear the memory which is you are not wanting. By using del you can clear variables, arrays, lists etc. So lets learn how we can delete this all by using del function: Clear a lists : You can clear list by this way:
lst=[1,2,3,4,5] del lst print(lst)
Output :Traceback (most recent call last): File "e:\python tutorial\test.py", line 672, in <module> print(lst) NameError: name 'lst' is not defined. Did you mean: 'list'?
Clear a variables : You can clear variables by this way:num=19 del num print(num)
Output :Traceback (most recent call last): File "e:\python tutorial\test.py", line 681, in <module> print(num) NameError: name 'num' is not defined. Did you mean: 'sum'?
How to clear Memory in Python
to clear Memory in Python just use gc.collect(). By using gc.collect() you can clear variables, arrays, lists etc. So lets learn how we can delete this all by using del function: Clear a lists : You can clear list by this way:
import gc mylst=[1,2,3,4,5] del mylst gc.collect() print(mylst)
Output :Traceback (most recent call last): File "e:\python tutorial\test.py", line 687, in <module> print(mylst) NameError: name 'mylst' is not defined
Clear a variables : You can clear variables by this way:import gc num=19 del num gc.collect() print(num)
Output :Traceback (most recent call last): File "e:\python tutorial\test.py", line 693, in <module> print(num) NameError: name 'num' is not defined. Did you mean: 'sum'?
python clear memory
To clear Memory in Python just use gc.collect(). By using gc.collect() you can clear variables, arrays, lists etc. So lets learn how we can delete this all by using del function: Clear a lists : You can clear list by this way:
import gc mylst=[1,2,3,4,5] del mylst gc.collect() print(mylst)
Output :Traceback (most recent call last): File "e:\python tutorial\test.py", line 687, in <module> print(mylst) NameError: name 'mylst' is not defined
Clear a variables : You can clear variables by this way:import gc num=19 del num gc.collect() print(num)
Output :Traceback (most recent call last): File "e:\python tutorial\test.py", line 693, in <module> print(num) NameError: name 'num' is not defined. Did you mean: 'sum'?
Method 1: Use del
By using del you can clear the memory which is you are not wanting. By using del you can clear variables, arrays, lists etc. So lets learn how we can delete this all by using del function:
Clear a lists :
You can clear list by this way:
lst=[1,2,3,4,5]
del lst
print(lst)
Output :
Traceback (most recent call last):
File "e:\python tutorial\test.py", line 672, in <module>
print(lst)
NameError: name 'lst' is not defined. Did you mean: 'list'?
Clear a variables :
You can clear variables by this way:
num=19
del num
print(num)
Output :
Traceback (most recent call last):
File "e:\python tutorial\test.py", line 681, in <module>
print(num)
NameError: name 'num' is not defined. Did you mean: 'sum'?
Method 2: Use gc.collect()
By using gc.collect() you can clear variables, arrays, lists etc. So lets learn how we can delete this all by using del function:
Clear a lists :
You can clear list by this way:
import gc
mylst=[1,2,3,4,5]
del mylst
gc.collect()
print(mylst)
Output :
Traceback (most recent call last):
File "e:\python tutorial\test.py", line 687, in <module>
print(mylst)
NameError: name 'mylst' is not defined
Clear a variables :
You can clear variables by this way:
import gc
num=19
del num
gc.collect()
print(num)
Output :
Traceback (most recent call last):
File "e:\python tutorial\test.py", line 693, in <module>
print(num)
NameError: name 'num' is not defined. Did you mean: 'sum'?
Conclusion
It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?
Also, Read