Garbage collection of python Advanced

 

Memory usage :

During operation of the program need to open up memory space, such as creating an object needs a memory space defined variables required memory space. It has opened up memory, but also there are bound to release the memory, otherwise it is not only into the brave yet? Even if there still will be opened with a release there is garbage in the short term memory.

 

Memory management :

, Memory and recovery needs to be done in open code in some languages, a typical example is the C language. C language opened up a memory: ptr = (int *) malloc (sizeof (int) * n); the release of a block of memory: free (ptr); this process is the need to write in the program before the memory in use (without wheels how to do , our own making). And Java, Python memory of this kind of language do not need to open up and recovery code implementation, there are mechanisms responsible for garbage collection GC in the background.

 

Python garbage collection mechanism used in two:

1. reference counting, mark clear

2. inter-generational recovery

 

Reference counting, mark Clear

python where every thing is an object, which is the core of a structure: PyObject

typedef struct_object {
    int ob_refcnt;
    struct_typeobject *ob_type;
} PyObject;

 

PyObject is each object must have content, which ob_refcnt is used as the reference count. When a new object is referenced, it will increase the ob_refcnt, when it is referenced object is deleted, it will reduce the ob_refcnt

#define Py_INCREF (OP) ((OP) -> ob_refcnt ++) // increment the count 
#define Py_DECREF (OP) \ // decrement the count 
IF (- (OP) -> ob_refcnt =! 0 ) \ 
     ; \ 
the else \ 
     __Py_Dealloc ( (PyObject *) (OP))

 

When you create an object immediately request Python memory to the operating system, while the C Python object structure in ob_refcnt value is set to 1.

If the re-assignment of a variable, the variable a case will point to a new object, the new object is a reference value 1, the old value of the reference count of the object will be set to 0.

 

When the python garbage collection mechanism found count memory object's reference value of 0, it was found that the debut of its own. Garbage collection will destroy the object, freeing memory.

Reference counting, mark clear this trick is very effective and can solve most of the objects recovered. But there are some cases that this move can not be resolved, and that is a circular reference . The so-called cycle applications means that two objects refer to each other, and became an island, is not associated with other objects.

 

In this case, the reference count is always 1, wants to release 'tommorr' No, because it is cited 'another', so the first release of 'another' to use 'tommor', but 'another' has been tommorr reference, can not be released. This is how to do it? Then need python garbage collection mechanism for inter-generational second run for recycling.

 

Inter-generational recovery

The main inter-generational collection mechanism 3 tricks:

第一:所有创建的对象都会加入到零代链表中,并扫描零代链表中所有的循环引用。如图发现两个对象的循环引用。

  

第二:将发现的循环引用的引用计数都减1。

当循环引用计数值为0之后,就被标记清除,垃圾回收第一规则就生效。如果清除一次引用计数还不为0咋办呢?那就是第三步

 

 第三步:将零代中引用计数不为0的对象加入到一代链表中,在一代链表中会重复零代中的循环计数减1操作。同时如果生命力顽强到一代链表都没处理的,那么会加入到二代链表。

 

 

以上讲到的是垃圾回收机制的大体思路,其实python的垃圾回收机制比以上描述的要复杂很多,很多深奥的东西可以看篇文章:https://www.cnblogs.com/pinganzi/p/6646742.html

 

Guess you like

Origin www.cnblogs.com/goldsunshine/p/11561612.html