Python garbage collection mechanism

Source: Internet
Author: User
Tags garbage collection

There are three main points:

1, reference count

2, Mark-Clear (for circular references)

3, generation of recycling

I. Garbage collection mechanism

Garbage collection in Python is based on reference counts and is supplemented by generational collections. The flaw in reference counting is the problem of circular referencing.
In Python, if an object has a reference count of 0,python, the memory of the object is reclaimed by the virtual machine.

#encoding =utf-8__author__ =' [Email protected] 'class classa  (): def  __init__print  ' object born , id:%s '%str (Hex (self)) def _ _del__print  ' object Del,id :%s '%str (Hex (self)) def f1< Span class= "Hljs-params" > (): while true:c1=classa () del C1             

Execution F1 () loops out such results, and the memory that the process consumes is basically unchanged

object born,id:0x237cf58object del,id:0x237cf58

c1=ClassA()An object is created, placed in 0x237cf58 memory, and the C1 variable points to this memory, when the reference count for this memory is 1
del c1, the C1 variable no longer points to 0x237cf58 memory, so the reference count of this block of memory is reduced by one, equal to 0, so the object is destroyed and the memory is freed.

  1. Case that causes reference count +1
    1. Objects are created, such asa=23
    2. object is referenced, such asb=a
    3. The object is passed into a function as a parameter, for examplefunc(a)
    4. Object as an element, stored in a container, such aslist1=[a,a]
  2. Case that causes reference count-1
      1. An alias for an object is explicitly destroyed, such asdel a
      2. The alias of the object is assigned a new object, such asa=24
      3. An object leaves its scope, such as the local variable in the Func function when the F function finishes executing (global variable does not)
      4. The container where the object resides is destroyed, or the object is deleted from the container

    Demo

     DefFunc(c,d):Print' in Func function ', Sys.getrefcount (c)-1Print' Init ', Sys.getrefcount (11)-1 A =11Print' After A=11 ', Sys.getrefcount (11)-1 B = APrint' After B=1 ', Sys.getrefcount (11)-1 func (11)Print' After Func (a) ', Sys.getrefcount (11)-1 List1 = [A,12,14]Print' After list1=[a,12,14] ', Sys.getrefcount (11)-1 a=12print ' after A=12 ', Sys.getrefcount (one)- 1 del a print ' After del a ', Sys.getrefcount (11) - 1 del b print ' after del b ', Sys.getrefcount (one)- 1 # list1.pop (0) # print ' Afte R pop List1 ', Sys.getrefcount (one)-1 del list1 print ' after Del List1 ', Sys.getrefcount (one)- 1 

    Output:

      init 24 after a=11 25 after b=1 26 in func function 28 after func (a) 26 after list1=[a,12, 14] 27 after a= 12 26 after del a 26 after del b 25  After del list1 24          

    Question: Why does the calling function make reference count +2

  3. View a reference count for an object

sys.getrefcount(a)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

Two. Circular references cause memory leaks
def f2():    while True: c1=ClassA() c2=ClassA() c1.t=c2 c2.t=c1 del c1 del c2 

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

object born,id:0x237cf30object born,id:0x237cf58

After creating the C1,C2, 0x237cf30 (c1 corresponds to memory, memory 1), 0x237cf58 (c2 corresponding memory, memory 2) The reference count of both blocks of memory is 1, executed c1.t=c2 and c2.t=c1 after, the reference count of the two memory becomes 2.
After Del C1, the reference count of the memory 1 object becomes 1, because it is not 0, so the memory 1 object is not destroyed, so the number of references to memory 2 is still 2, after Del C2, in the same vein, memory 1 object, memory 2 object reference count is 1.
Although all two of their objects can be destroyed, the garbage collector does not recycle them because of the circular references , which results in a memory leak.

Three. Garbage collection
deff3():    # print gc.collect()    c1=ClassA()    c2=ClassA()    c1.t=c2    c2.t=c1    del c1    del c2    print gc.garbage    print gc.collect() #显式执行垃圾回收 print gc.garbage time.sleep(10)if __name__ == ‘__main__‘: gc.set_debug(gc.DEBUG_LEAK) #设置gc模块的日志 f3()

Output:

 gc:uncollectable <ClassA Instance at 0230e918>gc:uncollectable <classa instance at 0230e940> Gc:uncollectable <dict 0230b810> gc:uncollectable <dict 02301ED0 >object born,id:0x230e918object born,id:0x230e9404             
    • Garbage collected objects are placed in the Gc.garbage list
    • gc.collect()Returns the number of unreachable objects, 4 equals two objects and their corresponding dict
    • There are three scenarios that trigger garbage collection:
      1. Call gc.collect() ,
      2. When the counter of the GC module reaches the threshold.
      3. When the program exits
Four. GC Module common function analysis

Garbage Collector Interface
The GC module provides an interface for developers to set options for garbage collection. As mentioned above, one flaw in managing memory using reference counting is circular referencing, and one of the main functions of GC modules is to solve the problem of circular references.

Common functions:
    1. Gc.set_debug (Flags)
      Set the debug log for the GC, which is generally set to GC. Debug_leak
    2. Gc.collect ([generation])
      Explicitly garbage collection, you can enter parameters, 0 means only check the first generation of objects, 1 for checking one, two generations of objects, 2 for checking one, two, three generations of objects, if not pass parameters, execute a full collection, which is equal to pass 2.
      Returns the number of unreachable (Unreachable objects) objects
    3. Gc.set_threshold (threshold0[, threshold1[, Threshold2])
      Set how often garbage collection is performed automatically.
    4. Gc.get_count ()
      Gets the counter that is currently automatically performing garbage collection, returning a list of length 3
Automatic garbage collection mechanism of GC module

You must import the GC module, and Is_enable () =true will not start automatic garbage collection.
The main function of this mechanism is to discover and dispose of inaccessible garbage objects.
Garbage collection = garbage Check + garbage collection
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 gc.get_count() obtained.
For example (488,3,0) , 488 it refers to the last generation of garbage checks, the number of Python allocated memory minus the number of freed memory, and the memory allocation, not the increase in the reference count . For example:

print gc.get_count()  # (590, 8, 0)a = ClassA()print gc.get_count()  # (591, 8, 0)del aprint gc.get_count() # (590, 8, 0)

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

GC modulo has a threshold for automatic garbage collection, that is, a gc.get_threshold tuple of length 3 obtained through a function, such as(700,10,10)
Each time the counter increases, the GC module checks to see if the added count reaches the threshold number, and if it does, it executes the corresponding algebraic garbage check and resets the counter
For example, suppose the threshold value is (700,10,10) :

    • When the counter is incremented, the (699,3,0) (700,3,0) GC module executes gc.collect(0) , checking the garbage of the generation object and resetting the counter to(0,4,0)
    • When the counter is incremented, the (699,9,0) (700,9,0) GC module executes gc.collect(1) , checking the garbage of one or two-generation objects and resetting the counter to(0,0,1)
    • When the counter is (699,9,9) increased (700,9,9) , the GC module executes gc.collect(2) , which is to check the garbage of one or two, three generations of objects, and reset the counter to(0,0,0)
Other
    1. If the two objects in the circular reference define __del__ methods, the GC module does not destroy the unreachable objects, because the GC module does not know which object should be called first __del__ , so for security reasons, the GC module places the object in Gc.garbage, but does not destroy the object.
Five. Application
      1. Avoid circular references in projects
      2. Introduction of GC module to initiate automatic cleanup of GC module object mechanism of circular reference
      3. Due to generational collection, centralized management of variables that require long-term use, and moving to the second generation as soon as possible, reducing the consumption of GC checks
      4. The only thing that the GC module cannot handle is that there are methods in the class of circular references, so it is necessary to __del__ avoid defining _ methods in the project, and _del__ if you must use this method and cause a circular reference, you need the code to explicitly invoke the gc.garbage object inside __del__ to break the deadlock

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.