Python garbage collection mechanism detailed

Source: Internet
Author: User

Referring to the "garbage collection mechanism" everyone will think of Java garbage collection, today is not about Java, but the Python programming language (http/ www.maiziedu.com/course/python-px/), why is there a garbage collection mechanism? The main purpose is to effectively free up memory, so python uses a relatively simple garbage collection mechanism, the following is a detailed description of the python garbage collection mechanism:

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 and the memory consumed is 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 the B Mutual references and no external references A with the B any of them, their reference count is 1 , but clearly should be recycled, examples:

A = {} # A has a reference of 1

the reference to B = {} # b is 1

a[' B ' = b # b 's reference is increased by 1, andb is quoted as 2

B[' a '] = a # A 's reference is increased by 1,a reference is 2

The reference to Del a # A is minus 1,a 's reference is 1

the reference to Del B # B is minus 1, and the reference for B is 1 .

In this example , the Del statement reduces The reference count for a and b and removes the variable name used for the reference. However, since two objects each contain a reference to an 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 trace (tracing) recycling technology, Objects are joined together by reference (pointers) to form a forward graph, and the object forms the node of the graph, and the reference relationship forms the edge of the graph. Starting from the root object, the objectis 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 and so on, because it is not possible to cause circular reference problems for strings and numeric objects. Python uses a doubly linked list to organize these container objects together.

Generational recycling

Generational recycling is a space-for-time operation in whichpython divides memory into different collections based on the object's survival time, each set called a generation, andpython divides memory into 3 " Generations (generation 0 ), the Middle Ages ( 1 generations), the Old generation (Section 2 ), 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 generation lists reaches the upper limit,thePython 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 object of the old age is the longest surviving object, 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 detailed

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.