close

How to delete multiple pandas (python) dataframes from memory to save RAM?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to delete multiple pandas (python) dataframes from memory to save RAM in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to delete multiple pandas (python) dataframes from memory to save RAM?

  1. How to delete multiple pandas (python) dataframes from memory to save RAM?

    When you do del i, you are deleting just the name i – but the instance is still bound to some other name, so it won't be Garbage-Collected.

  2. delete multiple pandas (python) dataframes from memory to save RAM

    When you do del i, you are deleting just the name i – but the instance is still bound to some other name, so it won't be Garbage-Collected.

Method 1

del statement does not delete an instance, it merely deletes a name.

When you do del i, you are deleting just the name i – but the instance is still bound to some other name, so it won’t be Garbage-Collected.

If you want to release memory, your dataframes has to be Garbage-Collected, i.e. delete all references to them.

If you created your dateframes dynamically to list, then removing that list will trigger Garbage Collection.

>>> lst = [pd.DataFrame(), pd.DataFrame(), pd.DataFrame()]
>>> del lst     # memory is released

If you created some variables, you have to delete them all.

>>> a, b, c = pd.DataFrame(), pd.DataFrame(), pd.DataFrame()
>>> lst = [a, b, c]
>>> del a, b, c # dfs still in list
>>> del lst     # memory release now

Method 2

In python automatic garbage collection deallocates the variable (pandas DataFrame are also just another object in terms of python). There are different garbage collection strategies that can be tweaked (requires significant learning).

You can manually trigger the garbage collection using

import gc
gc.collect()

But frequent calls to garbage collection is discouraged as it is a costly operation and may affect performance.

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read