In-depth understanding of python garbage collection mechanism

What is the garbage collection mechanism in python? Please sit down and explain briefly. The questions that most interviews will ask. Today I will briefly explain to you based on what I understand.

garbage collection mechanism

Garbage collection is mainly based on reference counting, supplemented by mark removal and generational recycling!

At this time, three technical points are involved, reference counting, mark clearing and generational recycling

 

reference count

Whenever you create an object, an ob_refcnt will be generated at the bottom layer, which is the reference count of the object. Every time you refer to the object, its reference count will increase by 1

a = 1

b = a 

a has a reference count of 2

When del b is used

It is to delete a reference count b. At this time, the reference count of a is 1. When the reference count is 0, garbage collection will be performed, the object will be destroyed, and the memory will be released.

However, there are bugs in reference counting, that is, memory leaks caused by circular references. How to solve them? At this time, it involves mark clearing and generational recycling.

#循环引用代码示范

v1 = [11,22,33]
v2 = [44,55,66]

v1.append(v2)
v2.append(v1)

del v1
del v2

mark clear

Mark clearing is used to solve the problem of circular references.

The principle is to open up a new address in python to store objects that may have circular references, and to scan for possible problems under certain circumstances, not only to scan and check the original object, but also to its child objects. Check the element, if there is a circular reference, if so, decrement its reference count by 1, if the reference count is 0, it will be garbage collected, if it is not 0, it will continue to be placed in memory. In short, it loops through each object, and the child elements of the object.

Here I mentioned that it is OK to scan under certain circumstances, and there will be two problems at this time! ! !

1. When to scan

2. High cost per scan

These two questions can introduce another topic ----- generational recycling

Generational recycling

Generational recycling, as the name suggests, divides objects that may have circular references into generations, that is, generation 0, generation 1, and generation 2

How often do you scan?

Generation 0: the number of objects in generation 0 is equal to 700 scans

Generation 1: objects in generation 0 are scanned 10 times, and objects in generation 1 are scanned once

Generation 2: objects in generation 1 are scanned 10 times, and objects in generation 2 are scanned once

Next, briefly describe the process-

Whenever there is a possibility of circular reference objects, the 0th generation is added by default. When the number of objects in the 0th generation is 700, all objects in the 0th generation will be scanned. If there is no problem, join the 1st generation, and if there is a problem Recycling, when the number of scans in the 0th generation is 10, the first generation will be scanned, and those without problems will be put into the second generation. When the number of scans in the first generation is 10, the second generation will scan!

This is an underlying principle of generational recycling

Guess you like

Origin blog.csdn.net/qwertyu111j/article/details/126003015