Menu Close

How to explicitly free memory in Python?

Sometimes, we want to explicitly free memory in Python.

In this article, we’ll look at how to explicitly free memory in Python.

How to explicitly free memory in Python?

To explicitly free memory in Python, we can use the gc.collect method.

For instance, we write

import gc

del foo
del bar
gc.collect()

to use the del operator to remove references of foo and bar from memory.

And then we call gc.collect to free up the memory.

Conclusion

To explicitly free memory in Python, we can use the gc.collect method.

Posted in Python, Python Answers