Understanding the Python garbage collection mechanism

Source: Internet
Author: User
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__ = ' kevinlu1010@qq.com ' class ClassA ():  def __init__ (self):    print ' Object born,id:% S '%str (Hex (self))  def __del__ (self):    print ' Object del,id:%s '%str (Hex (self)) def F1 (): 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 () Creates an object that is placed in 0x237cf58 memory, where the C1 variable points to the 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, the case that causes the reference count +1

    • Objects are created, such as a=23
    • object is referenced, such as B=a
    • The object is passed into a function as a parameter, for example, Func (a)
    • Object as an element, stored in a container, such as List1=[a,a]

2, resulting in reference count-1 case

    • The alias of the object is explicitly destroyed, for example del a
    • The alias of the object is assigned a new object, such as a=24
    • An object leaves its scope, such as the local variable in the Func function when the F function finishes executing (global variable does not)
    • The container where the object resides is destroyed, or the object is deleted from the container

Demo

def func (c,d):  print ' in Func function ', Sys.getrefcount (c)-1print ' init ', Sys.getrefcount (one)-1a = 11print ' after A=11 ', Sys.getrefcount (one)-1b = Aprint ' after B=1 ', Sys.getrefcount (one)-1func (one) print ' after Func (a) ', Sys.getrefcou NT (one)-1list1 = [A, B, 14]print ' after list1=[a,12,14] ', Sys.getrefcount (one)-1a=12print ' after A=12 ', Sys.getrefcount (one)-1del Aprint ' after del a ', Sys.getrefcount (one)-1del bprint ' after del b ', Sys.getrefcount (one)-# list1.pop (0) # print ' After Pop List1 ', Sys.getrefcount (one) -1del List1print ' after Del List1 ', Sys.getrefcount (11)-1

Output

Init 24after a=11 25after b=1 26in func function 28after func (a) 26after list1=[a,12,14] 27after a=12 26after del a 26afte R del B 25after del List1 24

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

3. View a reference count for an object

Sys.getrefcount (a) 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 corresponding memory, memory 1), 0x237cf58 (c2 corresponding memory, memory 2) both memory reference count is 1, after performing C1.T=C2 and C2.T=C1, the reference count of the two pieces of 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 (Ten) if __name__ = = ' __main __ ':  Gc.set_debug (GC. Debug_leak) #设置gc模块的日志  f3 ()

Output:

Gc:uncollectable 
 
  
   
  gc:uncollectable 
  
   
    
   gc:uncollectable 
   
    
     
    GC: uncollectable 
    
     
      
     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
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

5. 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 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 Gc.get_count () # (590, 8, 0) A = ClassA () print Gc.get_count () # (591, 8, 0) del aprint gc.get_count () # (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 a threshold for automatic garbage collection, that is, a tuple of length 3 obtained through the Gc.get_threshold function, for example (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 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 a 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), 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)

Other
If the __del__ method is defined by the two objects in the circular reference, the GC module does not destroy the unreachable objects because the GC module does not know which object should be called before the __del__ method, so for security reasons, the GC module places the object in Gc.garbage, but does not destroy the object.
Five. Application

    • Avoid circular references in projects
    • Introduction of GC module to initiate automatic cleanup of GC module object mechanism of circular reference
    • 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
    • The only thing that the GC module cannot handle is that the __del__ method is used in the circular reference class, so the project avoids defining the __del__ method, and if it is necessary to use the method and causes a circular reference, the code must explicitly invoke the __del__ of the object inside the gc.garbage to break the deadlock.

The above is the whole content of this article. We hope to help you with your study.

  • 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.