Python's garbage collection uses a binding mechanism that is based on the reference counting mechanism and is supplemented by the generational recycling mechanism, when the reference count of the object becomes 0 o'clock,
Object will be destroyed, except for objects created by default by the interpreter. (The reference count of the default object never becomes 0)
All counts refer to the case of +1:
One. The object is created:
1.a = 23
Here 23 This object is not new in memory, because when Python starts the interpreter, a small integer pool is created, and these objects between the -5~256
is automatically created to be loaded into memory waiting to be called; a = 23 is a reference to 23 for this integer object.
Execute code:
Import sys>>> a = 23>>> Sys.getrefcount (a)
Results: 15
23 This integer object currently has 15 references.
2.MyName ()
Class MyName (object):
Pass
Above, if the object is created without a reference operation, the reference count at this point is 0,myname () itself is not a reference.
Print (Sys.getrefcount (MyName ()))
Results: 1
Description: The result is 1 because the Sys.getrefcount (MyName ()) function also counts as a reference.
Two. The object is quoted;
A = 23345455== bprint(sys.getrefcount (b))print( Sys.getrefcount (c))
Results: bis
Note: Each assignment increases the number of references to the data operation, remembering that the reference is to data 23345455, such as the variable a,b,c, rather than the variable itself.
Three. The object is passed into a function as a parameter;
# added a reference to a = 23345455# adds a reference to B = a# adds a reference to C = b# added a reference to print(Sys.getrefcount (b)) # After completion of the reference destruction, reducing a reference # added a reference to print(Sys.getrefcount (c))
Description: The above code, the assignment operation adds 3 references to the data, and Sys.getrefcount (b) also adds a reference;
So why is the result of Sys.getrefcount (c) still 4? This is because when the function executes, a reference to the parameter is automatically destroyed, so print (Sys.getrefcount (b)) is deleted after the execution is complete .
Four. The object is stored in the container as an element;
# added a reference to a = 23345455# adds a reference to B == [A, b] # added 2 references to print (Sys.getrefcount (b)
Results: 5
There is also a case of all reference counts minus 1: Python's count Reference Analysis (ii)
Python count Reference analysis (i)