程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python super detailed explanation of memory management mechanism

編輯:Python

Catalog

What is memory management mechanism

One 、 Reference counting mechanism

Two 、 Data pool and cache

What is memory management mechanism

python When creating objects in , First, I will apply for the memory address , Then initialize the object , All objects are maintained in one

One is called refchain In a two-way circular list , Each data stores the following information :

 1. Pointer to the data before and after the data in the linked list
 2. Type of data
 3. Data values
 4. Reference count of data
 5. Length of data (list,dict..)

One 、 Reference counting mechanism

Increase in reference count :

1.1 Object created

1.2 Object is referenced by another variable ( Another name )

1.3 Objects are treated as elements , Put in container ( For example, it is put in the list as an element )

1.4 Object is passed as a parameter to a function

import sysa = [11,22] # Object created b = a # Object is referenced by another variable c = [111,222,333,a] # Objects are treated as elements , Put in container # Gets the reference count of the object print(sys.getrefcount(a)) # Object is passed as a parameter to a function

The final result of the execution is ,a This variable is referenced 4 Time

Reference count reduction :

The alias of the object is explicitly destroyed

An alias of an object is assigned to another object ( example : Like the original a=10, Changed to a=100, here 10 The reference count is reduced )

Object is removed from the container , Or the container is destroyed ( example : Object is removed from the list , Or the list is destroyed )

A reference leaves its scope ( The parameters passed in when calling the function , After the function runs , The reference to this parameter is destroyed )

import sysdel b # The alias of the object is explicitly destroyed b = 999 # An alias of an object is assigned to another object del c # List destroyed ( The container was destroyed )c.pop() # Delete the last data in the list ( Object is removed from the container ) Two 、 Data pool and cache

There are two types of data pools : A small pool of integers and Large integer pool

A small pool of integers (-5 To 256 Data between )

Operating mechanism :Python Automatically put -5~256 The integers of are cached into a small integer pool , When you assign these integers to variables , It's not going to happen again

Create objects , Instead, use the cache objects that have been created , When deleting references to these data , There's no recycling

beyond -5 To 256 The integer will not be in the cache , The object will be recreated

for example :

For exceeding -5 To 256 The integer will not be in the cache ,Python The object will be recreated , return id

# scene 1: Data as list , be not in -5~256 The scope of the >>> a = [11]>>> b = [11]>>> id(a),id(b)(1693226918600, 1693231858248) ========》 id Dissimilarity # Scene two : The data is an integer , stay -5~256 The scope of the >>> aa = 11>>> bb = 11>>> id(aa),id(bb)(140720470385616, 140720470385616) id equally # Scene three : Data not present -5~256 The scope of the >>> bb = -7>>> aa = -7>>> id(aa),id(bb)(1843518717904, 1843518717776) id Dissimilarity # Scene 4 : Data not present -5~256 The scope of the >>> a = 257>>> b = 257>>> id(a),id(b)(2092420910928, 2092420911056) id Dissimilarity

Large integer pool ( String resident pool / intern Mechanism )

advantage : When creating a new string object , We will first find out whether there are existing objects with the same value in the cache pool ( identifier , That is, it contains only numbers 、 Letter 、 String of underscores ), If there is , Take it directly ( quote ), Avoid frequent creation and destruction of memory , Improve efficiency

for example :

Data that is not in the identifier will not be cached ,Python The object will be recreated , return id

# scene 1:>>> a = '123adsf_'>>> b = '123adsf_'>>> id(a),id(b)(61173296, 61173296) ========》 id equally # Scene two : >>> b1 = '123adsf_?'>>> b2 = '123adsf_?'>>> id(b1),id(b2)(61173376, 61173416) id Dissimilarity

Caching mechanisms

about python Cache of commonly used built-in data types in :

float: cache 100 Objects

list: 80 Objects

dict: 80 Objects

set: 80 Objects

Tuples : According to the length of tuple data , The cache tuple length is 0-20 The object of

This is about Python This is the end of the super detailed article on memory management mechanism , More about Python Please search the previous articles of SDN or continue to browse the related articles below. I hope you can support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved