Definition of variables and assignment processing system that processes and issues of memory, this article discuss the application and release memory of two parts
In [1]:
name = 'admin'
print("name=", name)
name1 = name
print("name1=", name1)
In [2]:
name2 = 'admin'
print("name2=", name2)
In [3]:
print(id(name), name)
print(id(name1), name1)
print(id(name2), name2)
FIG: name defines a variable assigned to "admin", and then assign name1 name, the name name1 have the same value. Name2 then define another variable, the value is provided name2 "admin". That is, name, name1 and name2 values are "admin". The assignment process system runs as follows:
1. The system will apply for a memory, storage "admin"
2. The name, name1 and name2 common "admin" this memory
In [4]:
name2 = name
print(name == name2)
print(id(name) == id(name2))
In [5]:
name2 = "admin1"
print(id(name), name)
print(id(name2), name2)
print(name == name2)
print(id(name) == id(name2))
Python memory management there are three main mechanisms: the introduction of counting mechanism, garbage collection and memory pool mechanism.
The introduction of technology mechanism: within the python, to keep the memory of the object tracking through the introduction of technology, python internal records how many objects there are references that reference counting, when an object is created so that you create a reference count, when the object is no longer needed, the reference count of the object is 0, it will be garbage collected.
Garbage collection mechanism: When the memory in the memory section is no longer used, the garbage collector will clean out them. It will check the reference count for the object 0, and then remove it in the memory space.
Memory pool mechanism: in Python, in many cases the application memory is small memory, these small memory after the application, will soon be released, because the memory of these applications is not to create an object, and there is no object a memory pool mechanism. This means that a large number of Python will perform the operation malloc and free during operation, frequent switching between user mode and kernel mode, which will seriously affect the efficiency of Python. In order to accelerate the efficiency of Python, Python introduced a memory pool mechanism for the management of small memory application and release. Memory pool concept is to apply a pre-Yidingshuliang in memory, equal to the size of the block is held in reserve, when a new memory requirements, memory pool allocated to the start demand, then after a new application is not enough RAM. Doing the most significant advantage is the ability to reduce memory fragmentation, improve efficiency. There are many ways to achieve memory pool, and the scope is not the same performance.
The following analysis of the process of memory management and release of three steps:
In [6]:
name2 = 'admin1'
In [7]:
name1 = name2
print(id(name1), name1)
print(id(name2), name2)
print(id(name), name)
In [8]:
name = 'admin1'
print(id(name1), name1)
print(id(name2), name2)
print(id(name),name)
In [9]:
name = 'admin'
print(id(name1), name1)
print(id(name2), name2)
print(id(name),name)