< go > Python's garbage collection mechanism

Source: Internet
Author: User

The GC module of Python mainly uses "reference count" (reference counting) to track and recycle garbage. On the basis of the reference count, you can also solve the problem of circular references that can be generated by the container object through mark-clear (Mark and Sweep). Further increase the efficiency of garbage collection by " Generational recycling " (generation collection) for space Exchange time.

Reference counting mechanism:every thing in Python is an object, the core of which is a struct: Pyobject
1 typedef struct_object {2     int ob_refcnt;3     struct_typeobject *ob_type;4}pyobject;

Pyobject are the necessary content for each object, where ob_refcnt is counted as a reference. When an object has a new reference, its ob_refcnt is incremented, and when the object referencing it is deleted, its ob_refcnt is reduced.

1  # define PY_INCREF (OP)   (OP)->ob_refcnt++)          Increase Count 2  #define PY_DECREF (OP)      \/                         /Decrease Count         3       if (--(OP)->ob_refcnt! = 0)     4           ;         5       Else            6           __py_dealloc ((Pyobject *) (OP))

when the reference count is 0 o'clock, the object's life is over.

advantages of the reference counting mechanism:1. Simple2, real-time: Once there is no reference, the memory is released directly. Don't wait for a specific moment like any other mechanism.        Real-time also brings a benefit: the time it takes to process the recovered memory is allocated to the usual. disadvantages of the reference counting mechanism:1. Maintain reference count consumption resources2. Circular reference
List1 == [] list1.append (List2) list2.append (list1)
List1 and List2 refer to each other, and if there are no other objects referencing them, the reference count of List1 and List2 is still 1, and the memory consumed will never be recycled, which would be fatal. for today's powerful hardware, the disadvantage of 1 is acceptable, but circular references lead to memory leaks, destined Python will also introduce a new recycling mechanism.

As mentioned above, the recycling mechanism in Python is based on reference counting, which is supplemented by the two mechanisms of tag-purge and generational collection.

1. Mark-Clear mechanism

Mark-clear mechanism, as the name implies, first mark the object (garbage detection), and then clear the garbage (garbage collection).

Initially all objects are marked as white, and the root node objects are identified (these objects are not deleted), and they are marked black (indicating that the object is valid). Objects that are referenced by valid objects are marked dimmed (indicating that objects are up to date, but they are not checked), and gray is marked black when the object referenced by the gray object is checked. Repeat until no gray nodes exist. The last white node is the object that needs to be cleared.

2. Organization of Recycled objects

The advanced mechanism used here serves as a reference count helper mechanism for solving the recurring reference problems that arise. The circular reference only appears in the "internal existence of objects that can be referenced to other objects", such as: List,class, and so on.

In order to organize these recycled objects, you need to create a linked list. Naturally, each collected object needs to provide some more information, the following code is the object of the collection must appear.

1   is stored before the object structure. */2  typedef Union _GC_HEAD {3     struct {4          Union _gc_head *GC_ Next; 5          Union _gc_head *Gc_prev; 6          py_ssize_t gc_refs; 7      } GC; 8      a long double dummy;  /* Force worst-case Alignment */9  } pygc_head;

The actual structure of an object:

Each recycled object is connected by a pointer to Pygc_head, forming a list of all objects initialized in 1.

3, generation of recycling technology

Generational technology is a typical space-for-time technology, which is the key technology in Java. The idea is simple: the longer objects exist, the more likely they are to be garbage, and less to collect.

This idea can reduce the extra action that the tag-clearing mechanism brings. Generational is the collection of objects into a number of generations, each generation is a linked list (collection), generation of tokens-the time of the purge and the object in the generation

Positive proportional relationship of survival time

1/*** Global GC State ***/2   3 struct Gc_generation {4 Pygc_head Head;5int threshold; /* Collection Threshold */6int count; /* Count of Allocationsorcollections of younger7Generations * *8};//the structure of each generation9   Ten  #number of define num_generations 3//generations One  #define Gen_head (n) (&generations[n].head) A   -/* Linked lists of container objects */ -static struct gc_generation generations[num_generations] = { the/* Pygc_head, threshold, Count */ -{{{gen_head (0), Gen_head (0), 0}}, 700, 0}, -{{{gen_head (1), Gen_head (1), 0}}, 10, 0}, -{{{gen_head (2), Gen_head (2), 0}}, 10, 0}, +  }; -   +Pygc_head *_pygc_generation0 = gen_head (0);

From the above code you can see that Python has a total of three generations, and the threshold value of each generation represents the maximum number of objects that the generation accommodates. By default, when the 0 generation exceeds 700, or more than 10, the garbage collection mechanism is triggered.

The 0 generation trigger will clean up all three generations, the 1 generation trigger will clean up the generations, and the 2-generation trigger will only clean itself up.

A complete garbage collection process consists of the following four steps: link list establishment, identify root node, spam tag, garbage collection ~ The following blog is well elaborated: https://my.oschina.net/hebianxizao/blog/59896

Original address: http://www.cnblogs.com/hackerl/p/5901553.html

< go > Python's 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.