2.Python assignment and memory

Definition of variables and assignment processing system that processes and issues of memory, this article discuss the application and release memory of two parts

First, the application memory

 

When python define a variable, will apply to a memory object variable, the variable will be stored at the address in memory of the object

Such benefits are multiplexed together content objects, save memory space

In [1]:
name
= 'admin' print("name=", name) name1 = name print("name1=", name1)
name= admin
name1= admin
 
In [2]:
name2
= 'admin' print("name2=", name2)
NAME2 = admin
 
In [3]:
print
(id(name), name) print(id(name1), name1) print(id(name2), name2)
2951857458848 admin
2951857458848 admin
2951857458848 admin
 

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))
True
True
 
In [5]:
name2
= "admin1" print(id(name), name) print(id(name2), name2) print(name == name2) print(id(name) == id(name2))
2951857458848 admin
2951857622744 admin1
False
False
 

The above two python code validation in system variables share the same memory value.

1. When the value is "admin" name2 value of the name, with the same memory address name2 object name.

2. When the value is changed to name2 "admin1", the memory address of the object name2 will change.

 

 

 

Second, the release of memory

 

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:

 

Step 1: The values ​​are name1 and name2 becomes more "admin1", can be found, and name2 name1 objects are "admin1", name1 name2 printed object and the memory address are the same. The object name "admin", name printed memory address and the other two are different.

 
In [6]:
name2
= 'admin1'
 
In [7]:
name1
= name2 print(id(name1), name1) print(id(name2), name2) print(id(name), name)
2951857622744 admin1
2951857622744 admin1
2951857458848 admin
 

Step 2: The name of the object becomes more "admin1", then the new print discovered name, name1 and name2 address the same.

 
In [8]:
name
= 'admin1' print(id(name1), name1) print(id(name2), name2) print(id(name),name)
2951857622744 admin1
2951857622744 admin1
2951857622744 admin1
 

Step 3: Copy and reapply name is "admin", after printing out and found objects memory address step in the step of printing a name printed name of the memory address becomes different.

 
In [9]:
name
= 'admin' print(id(name1), name1) print(id(name2), name2) print(id(name),name)
2951857622744 admin1
2951857622744 admin1
2951857632064 admin

Guess you like

Origin www.cnblogs.com/King-Penguin/p/12074967.html