How to Explicitly Free Memory in Python – Python Memory Management Tutorial

Free of memory is releasing of memory used in the program if that is not going to use in the future We can do so using garbage collection.


What is Garbage Collection

Garbage collection clears the memory which is not going to be used in the future to faster the execution in a project for doing so we use gc.collect(). It is as important as collecting garbage from society as it decreases the diseases around, similarly the garbage collection or free Memory also makes the program more smooth

The garbage collection could be done implicitly at the end of every program. But at some times need to free the memory explicitly in between the execution of the program in case of large projects or programs where we might need that variable or object in some other contest.

Where it is a necessity to make the memory free and reuse it instant of keeping it without any reason or we can say that would be the waste of memory and increasing the execution timimg.

We execute it after making the variable or container discard by using del

For example-

my_list=[1, 3, 5, 54]
del my_list
del my_object

It will delete variables my_list and my_object both to make the memory free and use that memory space somewhere else

Need For Garbage Collection

Garbage collection is one of the ways to make our program execute faster and reduce memory usage and utilise the same memory for better use in future.

We delete and remove the variable or objects from memory and make the memory free for all those memories which is not going to be used in the future. It is a smart way of utilizing the memory spaces of Storage.

In small programs, it might not be useful but in large projects where lots of memory is used in that case memory utilization plays a major role in reducing memory storage and reducing execution time.

At the end of every program execution this is done implicitly but that is at the end wich does that affect the execution of currently executing program, And if the program is really large it really helps in reducing the complexity of execution.

Learn More about Garbage Collection or memory utilization: Python Memory Managements

and also Memory Collection

Leave a Comment

%d bloggers like this: