20191031: Python underlying mechanism

20191031: Python underlying mechanism

python bottom from three terms, namely:

  1. Reference counting mechanism
  2. Garbage collection
  3. Memory pool mechanism

Reference counting mechanism

Reference counting is to track objects in memory, all objects have the reference count, the reference count, and the number of ships than we thought, for immutable data (such as numbers and strings), the interpreter will be shared among different parts of the program memory in order to save memory, the object can be viewed by a number of applications sys.getrefcount (). as follows:

>>> a =19880924

>>> sys.getrefcount (19880924)

3

>>> b =19880924

>>> sys.getrefcount (19880924)

3

>>> c = 19880924

>>> sys.getrefcount (19880924)

19,880,924 citations can be seen not as the declaration of an object increases.

>>> c = 19880924

>>> sys.getrefcount (19880924)

3

>>> sys.getrefcount(c)

2

>>> d = c

>>> sys.getrefcount(c)

3

>>> e = c

>>> sys.getrefcount(c)

4

But if the object is referenced, the object of increasing the number of references

Memory storage mechanism similar to the following:

 

 

Increase in citations count:

1, a new object is assigned a name

2, which is placed in a container (e.g., a list or dictionary tuples)

Reducing the reference count of the case:

1, using the alias del statement of objects displayed destruction

2, a reference goes out of scope or reassigned

Garbage collection

  1. When an object's reference count is 0 when it was recovered out to garbage
  2. The interpreter will execute a loop detector on a regular basis, non-recyclable search access to an object and delete them.

a) If the two objects refer to each other, since each object contains an application to other objects, and therefore reference count is not zero, the object is not destroyed. In view of this situation applies to 2

Memory pool mechanism

Pymalloc mechanism: introducing memory pooling, managing a small memory for the application and release, Python all objects smaller than 256 bytes are pymalloc implemented using a dispenser, and large objects malloc system is used. For Python objects, such as integers, floating point and List, it has its separate private memory pools, between objects do not share their memory pool. This means that if you assign and release a large number of integer, integer memory for caching these can not be allocated to float.

python recovered memory into the memory pool rather than returned to the operating system.

Guess you like

Origin www.cnblogs.com/hyj691001/p/11773302.html