How does Python manage memory?

Time:2021-9-16

Python mainly manages memory through reference counting and memory pool mechanism.

1、 Reference counting mechanism

Python internally uses reference counting (recording how many references an object has) to keep track of objects in memory. When an object is created, the reference count of the object is increased by 1; When an object is destroyed, the reference count of the object becomes 0 and it is recycled as garbage.

Increase in reference count:

(1) Object is created, such as x = 4.

(2) assigned to othergameVariable, such as y = X.

(3) Is passed as an argument to a function, such as foo (x).

(4) As an element of a container object, such as a = [1, x, ’33’].

Decrease in reference count

(1) The reference of an object leaves its scope. For example, at the end of foo (x) function execution, the object reference count referenced by X is reduced by 1.

(2) References to objects are explicitly destroyed, such as del X or del y.

(3) The alias of the object is assigned to other objects, x = 789.

(4) Object is removed from the window object, www.sangpi.coma.remove (x).

garbage collection:

(1) The garbage collector reclaims objects with a reference count of 0 and clears the memory space occupied by these objects.

(2) When two objects refer to each other, they are collected by the garbage collector if they are not held by other references.

(3) The garbage collection mechanism also has a circular garbage collector that ensures the release of circular reference objects (a references B, B references a).

II. Memory pool mechanism
In Python, most of the time, the requested memory is a small block of memory, and the applications in these small blocks will be released soon, which means that the program will perform a large number of application and release operations during operation, affecting the execution efficiency of Python. In order to speed up the execution efficiency of python, python introduces a memory pool mechanism to manage the application and release of small memory.

All objects less than 256 bytes in Python use the allocator of the memory pool. In addition, some Python objects, such as integers, floating-point numbers, or lists, have independent memory pools that are not shared between objects. That is, if a large number of integers are released by allocation, the memory pool used to cache these integers will no longer be allocated to floating-point numbers.

How does Python manage memory? The above has made a detailed introduction for you. I hope it can help you.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]