In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces you what is the principle of Python garbage collection mechanism, the content is very detailed, interested friends can refer to, hope to be helpful to you.
one。 Garbage collection mechanism
Garbage collection in Python is dominated by reference counting, supplemented by generational collection. The flaw in reference counting is the problem of circular references.
In Python, if an object has 0 references, the Python virtual machine will reclaim the object's memory.
# encoding=utf-8__author__ = 'kevinlu1010@qq.com'class ClassA (): def _ init__ (self): print' object born,id:%s'%str (hex (id (self) def _ del__ (self): print 'object del,id:%s'%str (hex (id (self)) def F1 (): while True: c1=ClassA () del C1
Executing F1 () loops out such a result, and the memory consumed by the process is basically unchanged.
Object born,id:0x237cf58object del,id:0x237cf58
C1=ClassA () creates an object that is placed in 0x237cf58 memory, and the C1 variable points to that memory, where the reference count of this memory is 1.
After del C1, the C1 variable no longer points to 0x237cf58 memory, so the reference count of this memory is minus one, equal to 0, so the object is destroyed and the memory is freed.
Cases that result in a reference count of + 1
Objects are created, such as astat23
Object is referenced, for example, bicona
Object is passed as an argument to a function, such as func (a)
Object is stored as an element in a container, for example, list1= [a]
Causes the reference count to be-1
Demo
Def func (1print d): print'in func function', sys.getrefcount (c)-1print 'init', sys.getrefcount (11)-1a = 11print' after axiom 11A, sys.getrefcount (11)-1b = aprint 'after baccalaure1, sys.getrefcount (11)-1func (11) print' after func (a)', sys.getrefcount (11)-1list1 = [print 'after list1= [a dint 12] 14], sys.getrefcount (11)-1a=12print' after ajar 12' Sys.getrefcount (11)-1del aprint 'after del a', sys.getrefcount (11)-1del bprint' after del b', sys.getrefcount (11)-list1.pop (0) # print 'after pop list1',sys.getrefcount (11)-1del list1print' after del list1',sys.getrefcount (11)-1
Output:
Init 24after axiom 11 25after bliss 1 26in func function 28after func (a) 26after list1= [a mai 12m 14] 27after axiom 12 26after del a 26after del b 25after del list1 24
Question: why does calling a function make the reference count + 2
The alias of the object is explicitly destroyed, such as del a
The alias of the object is assigned to the new object, such as axiom 24
An object leaves its scope, for example, when the f function is finished, the local variable in the func function (the global variable will not)
The container in which the object is located is destroyed or the object is deleted from the container
View the reference count of an object
Sys.getrefcount (a) can view the reference count of the an object, but it is 1 higher than the normal count, because an is passed in when the function is called, which makes the reference count of a + 1
two。 Circular reference causes memory leak def f2 (): while True: c1=ClassA () c2=ClassA () c1.t=c2 c2.t=c1 del C1 del c2
Execute f2 (), and the memory consumed by the process will continue to increase.
Object born,id:0x237cf30object born,id:0x237cf58
After the creation of C1 memory c2, the reference counts of 0x237cf30 (memory corresponding to C1, marked as memory 1) and 0x237cf58 (memory corresponding to C2, recorded as memory 2) are both 1. After executing c1.t=c2 and c2.t=c1, the reference count of these two blocks of memory becomes 2. 5.
After del C1, the reference count of objects in memory 1 becomes 1. Because it is not 0, the objects in memory 1 will not be destroyed, so the number of references to objects in memory 2 is still 2. After del c2, the number of references to objects in memory 1 and objects in memory 2 is 1.
Although both of their objects can be destroyed, because of circular references, neither of them will be reclaimed by the garbage collector, resulting 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 () # explicitly execute garbage collection print gc.garbage time.sleep (10) if _ _ name__ = ='_ main__': gc.set_debug (gc.DEBUG_LEAK) # set gc module log f3 ()
Output:
Gc: uncollectable object born,id:0x230e918object born,id:0x230e9404
Garbage collected objects will be placed in the gc.garbage list
Gc.collect () returns an unreachable number of objects, where 4 equals two objects and their corresponding dict
There are three situations that trigger garbage collection:
1. Call gc.collect ()
two。 When the counter of the gc module reaches the threshold.
3. When the program exits,
IV. Analysis of common functions of gc module
Garbage Collector interface
The gc module provides an interface for developers to set options for garbage collection. As mentioned above, one of the drawbacks of managing memory using reference counting is circular references, and one of the main functions of the gc module is to solve the problem of circular references.
Common functions:
Gc.set_debug (flags)
Set the debug log of gc, which is generally set to gc.DEBUG_LEAK
Gc.collect ([generation])
For explicit garbage collection, you can enter parameters, 0 means to check only the first-generation objects, 1 represents to check the first-and second-generation objects, 2 represents to check the first -, second-and third-generation objects, if no parameters are passed, execute a full collection, that is, pass 2.
Returns the number of unreachable objects objects
Gc.set_threshold (threshold0 [, threshold1 [, threshold2])
Sets the frequency at which garbage collection is performed automatically.
Gc.get_count ()
Gets the counter that currently performs garbage collection automatically, and returns a list of length 3
Automatic garbage Collection Mechanism of gc Module
The import gc module is required, and is_enable () = True is required to start automatic garbage collection.
The main function of this mechanism is to find and deal with unreachable garbage objects.
Garbage collection = garbage inspection + garbage collection
In Python, the method of generational collection is adopted. The object is divided into three generations. at the beginning, when the object is created, it is placed in the first generation. if the changed object survives in the first generation of garbage inspection, it will be put into the second generation. Similarly, in the second generation of garbage inspection, the object survives and will be placed in the third generation.
There will be a counter with a list of 3 in the gc module, which can be obtained through gc.get_count ().
For example, 488 refers to the number of memory allocated by Python minus the number of memory freed from the previous generation of garbage checks, noting that it is memory allocation, not an increase in 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 number of times from the last second-generation garbage inspection and the first-generation garbage inspection, by the same token, 0 refers to the number of times from the last third-generation garbage inspection and the second-generation garbage inspection.
The gc module has a threshold for automatic garbage collection, that is, a tuple of length 3 obtained through the gc.get_threshold function, for example (70010)
Each time the counter is increased, the gc module checks whether the increased count reaches the threshold. If so, it performs the corresponding algebraic garbage check and then resets the counter.
For example, assume that the threshold value is (700 ~ 10 ~ 10):
When the counter is increased from (699) to (700), the gc module executes gc.collect (0), that is, checks the garbage of the generation object and resets the counter to (0).
When the counter is increased from (699) to (700), the gc module executes gc.collect (1), that is, checks the garbage of the first and second generation objects and resets the counter to (0memo).
When the counter is increased from (699) to (700), the gc module executes gc.collect (2), that is, checks the garbage of the first, second, and third generation objects and resets the counter to (0memo).
Other
If both objects have a _ _ del__ method defined in the circular reference, the gc module will not destroy these unreachable objects because the gc module does not know which object's _ _ del__ method should be called first, so for security reasons, the gc module will put the object in the gc.garbage, but will not destroy the object.
five。 Application
Avoid circular references in a project
Gc module is introduced to start the object mechanism of gc module to automatically clean up circular references.
Because of generation-by-generation collection, the variables that need to be used for a long time are managed centrally and moved to the second generation as soon as possible to reduce the consumption of GC checking.
The only thing that the gc module can't handle is that all the classes referenced by the loop have a _ _ del__ method, so you should avoid defining the _ _ del__ method in the project. If you must use this method and result in a circular reference, you need the code to explicitly call the _ _ del__ of the object in gc.garbage to break the deadlock.
On the principle of Python garbage collection mechanism is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.