1. In Python, both integers and short characters, Python caches these objects for re-use. When we create multiple references that are equal to 1, we actually make all of these references point to the same object.
A = 1b = 1print Hex (ID (a)) print hex (ID (b)) 0x1e6e038l0x1e6e038l
ID () to get memory address
2. References to Objects
From sys import Getrefcounta = [1, 2, 3]b = Aprint (Getrefcount (b)) A = 1print (Getrefcount (b))
Getrefcount can get the number of references to the object, it is important to note that when a reference is passed to Getrefcount () as a parameter, the parameter actually creates a temporary reference. As a result, Getrefcount () will get 1 more results than expected.
3. Garbage collection, count reduction
If the number of references to an object is 0, it is recycled.
When garbage collection occurs, Python cannot perform other tasks. Frequent garbage collection will greatly reduce python productivity. If there are not many objects in memory, it is not necessary to always start garbage collection. Therefore, Python automatically starts garbage collection only under certain conditions. When Python runs, it records the number of times it allocates objects (object allocation) and Unassigned Objects (object deallocation). Garbage collection starts when the difference between the two values is higher than a certain threshold.
We can view this threshold by using the Get_threshold () method of the GC module:
Import Gcprint (Gc.get_threshold ())
Return (700, 10, 10), followed by two 10 is the threshold associated with generational collection, which can be seen later. 700 is the threshold at which garbage collection starts. Can be reset by the Set_threshold () method in the GC. We can also start the garbage collection manually, that is, using Gc.collect ().
4. Generation of recycling
Python also uses the strategy of generational (generation) recycling. The basic assumption of this strategy is that the longer the surviving object, the less likely it is to become garbage in later programs. Our programs tend to produce a large number of objects, many objects quickly produce and disappear, but there are also some objects that are used for a long time. For the sake of trust and efficiency, we believe in the usefulness of these "longevity" objects, so we reduce the frequency of scanning them in garbage collection.
Python divides all the objects into 0,1,2 three generations. All new objects are 0-generation objects. When a generation of objects has gone through garbage collection and is still alive, it is grouped into next-generation objects. When garbage collection starts, all 0-generation objects are scanned. If 0 generations have been garbage collected for a certain number of times, then scan cleanup for 0 generation and 1 generation is initiated. When 1 generations have experienced a certain amount of garbage collection, the 0,1,2 is started, that is, all objects are scanned.
Import Gcgc.set_threshold (700, 10, 5)
can be set manually
Python's memory management