Garbage collection mechanism in Python

Source: Internet
Author: User

 When we declare an object, for example str= "abcdef", when we no longer use the Str object, this object is a dirty object, garbage object, but it still occupies the memory, after all, our computer memory is limited, so there should be a mechanism to recycle it and similar objects. Today's high-level languages such as java,c# have adopted a garbage collection mechanism, rather than a way for users to manage and maintain their own memory in c,c++. Self-management memory is extremely free, can arbitrarily apply for memory, but like a double-edged sword, for a large number of memory leaks, dangling pointers and other bugs buried hidden trouble. For a string, list, class, or even numeric value is an object, and positioning a simple and easy-to-use language, nature will not let the user to deal with how to allocate memory collection. Python also uses the same garbage collection mechanism as Java, but not the same: Python is based on the reference counting mechanism, the collection mechanism supplemented by the strategy.

One, reference counting machine System : This is an important mechanism for the use of Python garbage collection.

Every thing in Python is an object, the core of which is a struct:PyObject

typedef struct_object {    int  ob_refcnt;     *Ob_type;} 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. When the reference count is 0 o'clock, the object's life is over.

advantages of the reference counting mechanism:
    • Simple
    • 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:
    • Maintain reference count consumption resources
    • Circular references
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. (Collection of Generations)

1, the case that causes the reference count +1
    • 对象被创建,例如a=23
    • 对象被引用,例如b=a
    • 对象被作为参数,传入到一个函数中,例如func(a)
    • 对象作为一个元素,存储在容器中,例如list1=[a,a]
2, resulting in reference count-1 case
    • 对象的别名被显式销毁,例如del a
    • 对象的别名被赋予新的对象,例如a=24
    • 一个对象离开它的作用域,例如f函数执行完毕时,func函数中的局部变量(全局变量不会)
    • 对象所在的容器被销毁,或从容器中删除对象

3. View a reference count for an object
Import"hello"#2

You can view the reference count of a object, but it is 1 larger than the normal count, because a is passed in when the function is called, which gives a reference count of +1.

4. Circular references cause memory leaks

ImportGCclassClassA ():def __init__(self):Print('Build Object, id:%s'%str (ID ( self)))defF2 (): whileTRUE:C1=ClassA () C2=ClassA () c1.t=C2 c2.t=C1delC1delC2#python turns on garbage collection by default and can be turned off by the following codegc.disable () f2 ( )

Executes F2 (), and the memory that the process consumes grows.

    • After the C1,C2 has been created, the reference count for both blocks of memory is 1, c1.t=c2 and c2.t=c1 after that, the reference count for both blocks of memory becomes 2.
    • After Del C1, the reference count becomes 1, and because it is not 0, the C1 object is not destroyed, so the C2 object's reference count is 1.
    • Python turns on garbage collection by default, but because the program has turned it off, the garbage collector does not recycle them, so it causes a memory leak.
There are three scenarios that trigger garbage collection:
    1. Automatically recycle garbage when the counter of the GC module reaches the threshold
    2. Call Gc.collect (), Recycle garbage manually
    3. When the program exits, the Python interpreter will recycle the garbage.

Second, the collection mechanism of generation:

In Python, the method of generational collection is used. The object is divided into three generations, the beginning, the object at the time of creation, placed in a generation, if in a generation of garbage inspection, the change to survive, will be placed in the second generation, the same time in a second generation of garbage inspection, the object survived, will be placed in three generations.

The GC module will have a counter with a 3-length list, which can be obtained by Gc.get_count ().

For example (488,3,0), where 488 is the distance from the last generation of garbage checks, the number of Python allocated memory minus the number of freed memory, note that the memory allocation, rather than the increase in the reference count. For example:

Print # (590, 8, 0)a = ClassA ()print#  (591, 8, 0)del  aprint  #  (590, 8, 0)

3 refers to the last second generation of garbage inspection, a generation of garbage inspection, the same,0 is the distance from the last three generations of garbage inspection, the second generation of garbage check the number of times.

GC modulo fast has an automatic garbage collection 阀值 , that is, through the Gc.get_threshold function to obtain a length of 3 tuples, such as (700,10,10) each time the increase of the counter, the GC module will check whether the increase in the count of the number of thresholds, if so, The corresponding algebraic garbage check is performed, and then the counter is reset

For example, suppose the threshold is (700,10,10):

When the counter is increased from (699,3,0) to (700,3,0), the GC module executes gc.collect (0), which checks the garbage of the generation object and resets the counter to (0,4, 0) when the counter is increased from (699,9,0) to (700,9,0), The GC module executes gc.collect (1), which checks the garbage of the one or two-generation object and resets the counter to (0,0,1) when the counter is increased from (699,9,9) to (700,9,9), and the GC module executes Gc.collect (2). That is, check the garbage of one or two, three generations of objects, and reset the counter to (0,0,0)

Garbage collection mechanism in Python

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.