Detailed explanation of Python garbage collection mechanism: reference counting and circular garbage collector

Python garbage collection mechanism

The Python programming language adopts an automatic garbage collection mechanism, which can automatically release objects that are no longer needed and return the memory occupied by them to the operating system for use by other programs. This relieves the programmer's burden to a certain extent, as they do not need to manually free the memory.

Reference counting mechanism

Python primarily uses reference counting as part of its garbage collection mechanism. Every object has a reference counter that records how many variables reference that object. When the reference counter reaches zero, it means that no variable refers to the object, and the object becomes a garbage object and will be automatically deleted by the garbage collection mechanism.

Here is a simple example to demonstrate how the reference counting mechanism works:

class Person:
    def __init__(self, name):
        self.name = name

    def __del__(self):
        print(f'{
      
      self.name}对象被删除了')

# 创建两个对象,并相互引用
p1 = Person('Alice')
p2 = Person('Bob')

# 打印两个对象的引用计数器
print(sys.getrefcount(p1))  # 输出结果为2,包括p1和参数传递中的临时引用
print(sys.getrefcount(p2))  # 输出结果为2

# 断开对象之间的相互引用
p1 = None
p2 = None

# 观察析构函数的调用情况

In the above example, we defined a Person class, created two objects p1 and p2, and referenced each other. Use sys.getrefcount()the function to get the reference count of an object. Finally, we set the references of p1 and p2 to None, breaking the mutual references between them. At this time, you can observe that __del__the method is called and a message that the object is deleted is printed.

Circular garbage collector

In addition to the reference counting mechanism, Python also uses a cyclic garbage collector (Cycle GC) to deal with circular references. A circular reference occurs when two or more objects refer to each other without other objects referencing them, making the objects inaccessible. The cyclic garbage collector periodically scans objects in memory to detect cyclic references and clean up these inaccessible objects.

The following is an example of a circular reference:

class A:
    def __init__(self, b):
        self.b = b

class B:
    def __init__(self, a):
        self.a = a

# 创建两个对象,并相互引用形成循环引用
a_obj = A(None)
b_obj = B(a_obj)
a_obj.b = b_obj

# 对象无法通过其他引用访问到
a_obj = None
b_obj = None

# 手动进行垃圾回收
gc.collect()

In this example, we create two objects a_objand b_obj, which refer to each other forming a circular reference. Even if their references are set to None, these objects cannot be accessed through other references. Manual calls gc.collect()can force garbage collection and clean up these inaccessible objects.

It is important to note that in most cases, manual garbage collection operations are not required. Python's garbage collection mechanism will be executed automatically when appropriate. Manually calling garbage collection is usually used in some special situations, such as when a large number of objects are created and destroyed, to optimize memory usage.

By understanding Python's garbage collection mechanism, programmers can better manage memory and improve the efficiency and maintainability of their code.

summary

Python's circular garbage collector works when there are circular references between objects. The cyclic garbage collector uses another strategy called mark-and-sweep. Here is how the cyclic garbage collector works:

  1. Marking phase: Starting from the root object, the cyclic garbage collector traverses all reachable objects and marks them as "alive".
  2. Cleanup phase: The cyclic garbage collector scans all objects in the heap memory, determines unmarked objects as garbage objects, and reclaims their memory space.

The timing of execution of the cyclic garbage collector is automatically controlled by the Python interpreter. When certain conditions are reached, such as memory usage exceeding a threshold, when the CPU is idle, etc., the Python interpreter will trigger the execution of the cyclic garbage collector.

It should be noted that the work of the cyclic garbage collector will cause certain performance overhead. Therefore, when writing code, we should try to avoid circular references to reduce the frequency and overhead of garbage collection.

In addition, Python also provides gcmodules that allow us to have more granular control over garbage collection. By adjusting gcthe relevant parameters of the module, we can change the behavior of garbage collection, such as disabling the cyclic garbage collector, setting the threshold for garbage collection, etc. For specific usage methods, please refer to the official Python documentation.

To sum up, Python's garbage collection mechanism mainly includes reference counting and circular garbage collectors. Reference counting is used to track the references of an object. When no variables refer to the object, the object will be released. The cyclic garbage collector handles the situation where circular references exist, marking and clearing inaccessible objects. Through these two mechanisms, Python can automatically manage memory and perform garbage collection, reducing the burden on programmers.

Detailed explanation and practical operation

1. Garbage problem in the program

Garbage will be generated during the running of the program, and this garbage will affect the performance of the program. Therefore, we need to clean up these garbage in time.

2. Definition of garbage

In a program, objects that are not referenced are considered garbage. When there are too many garbage objects, it will affect the performance of the program.

3. Automatic garbage collection mechanism

In Python, there is an automatic garbage collection mechanism. It automatically deletes objects that are not referenced, eliminating the need to manually handle garbage collection.

4. Example: Use del method to delete garbage objects

The following is a sample code that shows how to delete garbage objects using the del method.

class A:
    def __init__(self):
        self.name = 'A类'

    # del是一个特殊方法,它会在对象被垃圾回收前调用
    def __del__(self):
        print('A()对象被删除了~~~',self)

a = A()
b = a # 又使用一个变量b,来引用a对应的对象

print(a.name)

5. Handle garbage collection manually

If you wish to handle garbage collection manually, you can set the object's reference to None or use the del statement to delete the reference. Here is the sample code:

# 将a设置为了None,此时没有任何的变量对A()对象进行引用,它就是变成了垃圾
a = None
b = None

6. End the program

Finally, you can add a line of input statements at the end of the code to wait for user input to exit after the program is executed.

input('回车键退出...')

7. Automatic processing of garbage collection

Python's garbage collection mechanism will automatically delete objects that are not referenced without manual processing. Here is sample code:

# 定义一个类A
class A:
    def __init__(self):
        self.name = 'A类'

    # del是一个特殊方法,它会在对象被垃圾回收前调用
    def __del__(self):
        print('A()对象被删除了~~~',self)

# 创建一个A类的实例a并引用它
a = A()

# 打印a的名称属性值
print(a.name)

# 删除a的引用
a = None

# 程序运行结束后,会自动调用垃圾回收机制删除没有被引用的对象

When the program ends, Python will automatically call the garbage collection mechanism to delete unreferenced objects. You can see that in the sample code, when the reference of a is set to None, the object a becomes a garbage object and will eventually be deleted by the garbage collection mechanism.

8. End the program

Finally, you can add a line of input statements at the end of the code to wait for user input to exit after the program is executed.

input('回车键退出...')

Recommended Python boutique columns


Basic knowledge of python (0 basic introduction)

[Python basic knowledge] 0.print() function
[python basic knowledge] 1. Data types, data applications, data conversion
[python basic knowledge] 2. if conditional judgment and condition nesting
[python basic knowledge] 3.input() Functions
[Basic knowledge of python] 4. Lists and dictionaries
[Basic knowledge of python] 5. For loops and while loops
[Basic knowledge of python] 6. Boolean values ​​and four types of statements (break, continue, pass, else)
[Basic knowledge of python] 7. Practical operation - Use Python to implement the "Word PK" game (1)
[Python basic knowledge] 7. Practical operation - Use Python to implement the "Word PK" game (2)
[Python basic knowledge] 8. Programming thinking: how to Solving problems - Thinking Chapter
[Basic Knowledge of python] 9. Definition and calling of functions
[Basic Knowledge of Python] 10. Writing programs with functions - Practical Chapter
[Basic Knowledge of Python] 10. Using Python to implement the rock-paper-scissors game - Function Practice Operation Chapter
[Python Basics] 11. How to debug - Common error reasons and troubleshooting ideas - Thinking Chapter
[Python Basics] 12. Classes and Objects (1)
[Python Basics] 12. Classes and Objects (2)
[Python Basics] Knowledge] 13. Classes and objects (3)
[Basic knowledge of python] 13. Classes and objects (4)
[Basic knowledge of python] 14. Building a library management system (practical operation of classes and objects)
[Basic knowledge of python] 15. Coding basic knowledge
[Basic knowledge of python] 16. Fundamentals of file reading and writing and operations
[Basic knowledge of python] 16. Python implementation of "Ancient poem dictation questions" (File reading, writing and coding - practical operation)
[Basic knowledge of python] 17. The concept of modules and How to introduce
[python basics] 18. Practical operation - using python to automatically send mass emails
[python basics] 19. Product thinking and the use of flow charts - thinking
[python basics] 20. python implementation of "what to eat for lunch" ( Product Thinking - Practical Operation)
[Basic knowledge of python] 21. The correct way to open efficiently and lazily - Graduation
[python file processing] Reading, processing and writing of CSV files
[python file processing] Excel automatic processing (using openpyxl)
[python file processing]-excel format processing


python crawler knowledge

[python crawler] 1. Basic knowledge of crawlers
[python crawler] 2. Basic knowledge of web pages
[python crawler] 3. First experience with crawlers (BeautifulSoup analysis)
[python crawler] 4. Practical crawler operation (dish crawling)
[python crawler] 5 .Practical crawler operation (crawling lyrics)
[python crawler] 6. Practical crawler operation (requesting data with parameters)
[python crawler] 7. Where is the crawled data stored?
[python crawler] 8. Review the past and learn the new
[python crawler] 9. Log in with cookies (cookies)
[python crawler] 10. Command the browser to work automatically (selenium)
[python crawler] 11. Let the crawler report to you on time
[python crawler] 12. Build your crawler army
[python crawler] 13. What to eat without getting fat (crawler practical exercise)
[python crawler] 14. Scrapy framework explanation
[python crawler] 15. Scrapy framework practice (popular job crawling Take)
[python crawler] 16. Summary and review of crawler knowledge points

Guess you like

Origin blog.csdn.net/qq_41308872/article/details/132856848