*
It is generally related to the following two modules
import gc
import sys
High level languages generally have garbage collection mechanism , among c,c++ It uses the way that the user manages and maintains the memory , This way is more free , However, if it is not recycled properly, it will also cause memory leakage and other problems . and python It mainly adopts the reference counting mechanism , sign - The strategy of cleaning and generational collection .
*
Memory garbage collection timing :
1. call gc.collect();
2. When gc When the module's counter reaches the threshold ;
3. When the program exits .
<> Reference count
python Everything is an object , therefore python The underlying counting structure can be abstracted as :
Reference count structure {
Reference count
Referenced objects
}
When will an object be deleted : Reference count =0
When is reference count +1, Under what circumstances -1, When the number of references is 0 Time , Sure is the time to recycle .
Reference count +1 The situation
1, When an object is created , for example a=“hello zzy”
2, The object is copy When quoting , for example b=a, here a Reference count +1
3, Object is used as a parameter , When passed into a function
4, Object as a child element , When stored in a container , for example list=[a,b]
Reference count -1 The situation
1, The alias is destroyed by the object , for example del a
2, The object reference is given a new object , for example b=c, here b Reference count -1( Cross reference count +1 In the case of the second point )
3, A function leaves its scope , For example, function execution is complete , The reference count of its reference parameter -1
4, The container of the object is destroyed , Or remove it from the container .
Methods of counting reference count :
import sys
print(sys.getrefcount(xxx quote ))
<> sign - eliminate
If single use reference count recycling , There's a problem that can't be solved :
arr1 = [1] arr2 = [2] arr1.append(arr2) arr2.append(arr1)
print(sys.getrefcount(arr1)) print(sys.getrefcount(arr2))
Reference counting alone cannot be deleted now
Need to use mark clearing method :
All can't be downloaded from 0 All memory found by a level reference is marked as cleanable , They'll be recycled and cleaned up
<> Generational collection
*
Through the top 【 Reference count 】 and 【 sign - clear 】 method , It's already guaranteed to recycle the garbage , But there is another problem , Garbage collection will traverse all references to identify whether garbage collection is needed , System performance will be affected , that python To this end, we have made a way to optimize the performance : Generational recycling mechanism
*
Python Divide all objects into 0,1,2 Three generations . All new objects are 0 Surrogate object . When a generation of objects has experienced garbage collection , Still alive , Then it's classified as the next generation of objects . When garbage collection starts , We're going to scan all of them 0 Surrogate object . If 0 After a certain number of garbage collection , So start it. Yes 0 Daihe 1 Generation scan clean . When 1 Generation also experienced a certain number of garbage collection , Then it will start. Yes 0,1,2, All objects are scanned .
import gc
print(gc.get_threshold())
return (700, 10,
10), The two in the back 10 Is the threshold associated with generational recycling , You can see it in the back .700 This is the threshold at which garbage collection starts . in other words , Memory 0 Algebra to 700, recovery 0 Replace garbage once , each 10 second 0 Recycling on behalf of garbage , Will cooperate 1 second 1 Generation of garbage collection ; And every 10 second 1 Generation of garbage collection , There will be 1 Secondary 2 Recycling on behalf of garbage .
Such a mechanism can ensure that frequently used objects will not be repeatedly detected for recycling , Guaranteed performance .
It can be used set_threshold() To adjust the threshold to control the recycling frequency :
import gc
gc.set_threshold(700, 10, 5)
Technology
Daily Recommendation