Categories
Python Answers

How to explicitly free memory in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *