Python Tutorial: Python Garbage Collection - Python Variables #23

preview_player
Показать описание

Python Garbage Collection
One of the nicest things about the Python programming language is the Python garbage collection feature which will monitor and free up memory automatically. If we had to manage memory in Python programs we would devote more time allocating memory in our programs then we would be writing the program. In this Python tutorial, we will have a close look at how Python manages memory with its garbage collection algorithm.

What is Garbage Collection?
Garbage collection is a process whereby a program runs some code to clean up unused memory. In previous Python tutorials, we have discussed that the blocks of memory in Python are designated as objects. When these objects are not required anymore than the garbage collection system jumps in and removes those objects to clear up memory for more objects. This is all automatic so we are not required to initiate the garbage collection system to free up memory.

How Does The Garbage Collection Work?
When an object is no longer used then the garbage collection algorithm will remove the object. This is where the reference counter becomes involved. If the object is referenced then the reference counter increments up by one and when the reference to that object is removed then the counter increments down. When the objects reference counter is at zero(No References To The Object) then that object will be deleted. The reference counter method is great but there are cases where this method of removing objects doesn't always work.

Reference Cycles

Sometimes the objects reference counter never goes to zero and there is a lack of physical reference to an object. This happens when an object reference itself and this is referred to as a reference cycle. In this case, Python relies on a schedule for garbage collection to run. Each time a reference is added or removed the system will count the occurrence and when the number of occurrences reaches a set number then the garbage collection will run and remove reference cycles.
Рекомендации по теме
Комментарии
Автор

I hope this video has image guide thing to explain this better. But i like this tutorials !. But.. i didnt understand fully this video. :(

kyoheo
Автор

Not much information, you may present how you garbage collect b or a

danlan
Автор

Informative. Do you have any in details video on gc?

arnabkumarpan
Автор

How do you the people in the comments not understand this? When a variable is assigned to a value, it is referencing the memory address in RAM that holds that value. Each time a variable references a particular memory address, the reference counter of that memory address increments. if the value of the memory address (that a variable references) is overwritten, the python garbage collector destroys all references of the original memory address, thus freeing memory.

melkenhoning
Автор

Dude, I'm sure you've tried your best but I didn't like your explanation, little information. Plus, you speak very slowly, I had to increase the video speed. I would suggest you to use some class-object to explain garbage collection. For example, you could use __del__ method (destructor) which is invoked when an instance is about to be destroyed.
Good luck!

parvizkarimli
Автор

near about 8 min video and with this little information, please don't waste people time, people already know that gc runs to clean up and referenced things, but not enough knowledge is provided here .

ABHISHEK_
visit shbcf.ru