PYTHON__ Advanced: GC garbage Collection related

Source: Internet
Author: User

Python's garbage collection mechanism is based on the reference count, plus tag-clear, generational collection and other auxiliary methods, if you want to open the GC function, need to import GC module, then Gc.enable () opened this function, Close is gc.disable ().

View a reference count for an object: Sys.getrefcount () is always more than the actual +1, because Sys.getrefcount () also calls it once.

The case where the reference count is +1:

I. Object is created

Two. Object is referenced

Three. The object is passed into a function as a parameter

Four. Object as an element, stored in a container

The case of the reference count being-1:

I. Aliases of objects are assigned to new objects

Two. An alias for an object is explicitly destroyed

Three. An object leaves its scope

Four. The container in which the object is located is destroyed, or the object is deleted from the container

There are several methods in the GC module, which are found online below:

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 () # (59080= ClassA () print Gc.get_count () # (591  80) del aprint Gc.get_count () # (59080)

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.

PYTHON__ Advanced: GC garbage Collection related

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.