Python garbage collection mechanism

Source: Internet
Author: User

Reference count python The default garbage collection mechanism is reference count, and each object maintains a ob_ref field. Its advantage is that the mechanism is simple, and when a new reference points to the object, the reference count reference count

Python's default garbage collection mechanism is reference counting, and each object maintains a ob_ref field. Its advantage is that the mechanism is simple, when the new reference point to the object, the reference count plus 1, when an object's reference is destroyed minus 1, once the object's reference count is 0, the object is immediately reclaimed, the memory will be freed. The disadvantage is that additional space is required to maintain the reference count, but the main problem is that it does not solve the "circular reference".

What is a circular reference? A and B reference each other, and no external references to any of A and B, their reference count is 1, but clearly should be recycled, for example:

123456   ={ } # a 的引用为 1  = { } # b 的引用为 1  a[‘b‘=# b 的引用增 1,b的引用为2  b[‘a‘=# a 的引用增 1,a的引用为 2  del# a 的引用减 1,a的引用为 1  del# b 的引用减 1, b的引用为 1

In this example, the DEL statement reduces the reference count of A and B and removes the variable name used for the reference, but since the two objects each contain a reference to the other object, the reference count is not reduced to zero, although the last two objects cannot be accessed by name. So this object will not be destroyed, it will always reside in memory, which will cause a memory leak. To solve the circular reference problem, Python introduces two GC mechanisms, tag-purge and generational recycling.

Mark Clear

Tag-Clear (Mark--sweep) is a garbage collection algorithm based on tracing (tracing) recycling technology, which is connected by reference (pointer) to form a forward graph, the object forms the node of the graph, and the reference relation forms the edge of the graph. Starting from the root object, the object is traversed along a forward edge, and objects that can be reached are marked as useful objects, and objects that are unreachable are objects to be purged. The so-called root object is a reference to the global Reference object and the function stack, which references objects that cannot be deleted.

The markup cleanup algorithm, as a Python-assisted garbage collection technique, mainly deals with container objects such as list, dict, tuple,instance, etc., because it is not possible to cause circular reference problems for strings or numeric objects. Python uses a doubly linked list to organize these container objects together.

Generational recycling

Generational recycling is a space-for-time operation, and Python divides the memory into different collections based on the object's survival time, each of which is called a generation, and Python divides the memory into 3 "generations", namely the younger generation (No. 0 generation), the middle age (1th generation), the old generation (2nd generation), They correspond to 3 linked lists, their garbage collection frequency and the object's survival time increases and decreases. The newly created objects are assigned to the young generation, when the total number of young linked lists reaches the upper limit, the Python garbage collection mechanism is triggered, the objects that can be recycled are recycled, and the objects that are not recycled are moved to the Middle Ages, and so on, the objects in the old age are the objects that have survived the longest. Even within the life cycle of the entire system. At the same time, generational recycling is based on the technology of Mark removal.

Generational recycling also acts as a Python-assisted garbage collection technique for handling those container objects

Python garbage collection mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.