Python's memory management and garbage collection mechanism learning

Source: Internet
Author: User
Tags shallow copy

A. Python memory request:

1, Python memory management is divided into six layers: the bottom two layers have OS control. The third layer is to call C's malloc and free for memory control. Layer fourth layer fifth is the memory pool of Python. The topmost layer allows us to manipulate the Python object directly.

2, Python Application object time less than 256Byte bytes request back directly using Python's own memory allocation system, when greater than 256Byte will call malloc directly allocated a 256k of large memory space. When the memory space is freed, it is reclaimed into the memory pool instead of directly calling free.

3. Different shades of Copy (ID memory address):

(1) deep copy (basic data type: integer, floating point, String, Ganso, list, etc.) "a=x; b=a; a changes to B invariant" The two IDs will be different after a change, the same as before. (A new address will be added)

1Python2.7.9(Default, DecTen  the, A: -:Geneva) [MSC v. the  -bit (AMD64)] on Win322Type" Help","Copyright","credits"Or"License"  for  Moreinformation.3>>> A =14>>> B =a5>>>ID(a)6 6579496L7>>>ID(b)8 6579496L9>>> A =2Ten>>>ID(a) One 6579472L A>>>ID(b) - 6579496L ->>>

(2) Shallow copy (data structure type: dictionary, etc.) "a=x; b=a; a changes to B is also the case" after a change the ID is still the same, before the same, and before and after the modification of the ID unchanged. (no new address added)

1Python2.7.9(Default, DecTen  the, A: -:Geneva) [MSC v. the  -bit (AMD64)] on Win322Type" Help","Copyright","credits"Or"License"  for  Moreinformation.3>>> a = [1,2,3]4>>> B =a5>>>ID(a)6 45528520L7>>>ID(b)8 45528520L9>>> a = [1,2,3,4]Ten>>>ID(a) One 45643272L A>>>ID(b) - 45528520L ->>>a the[1,2,3,4] ->>>b -[1,2,3] ->>> A = {"a":1,"b":2} +>>> B =a ->>> +>>> A>>>ID(a) at 45672376L ->>>ID(b) - 45672376L ->>> a["a"] =3 ->>>ID(a) - 45672376L in>>>ID(b) - 45672376L to>>>b +{'a':3,'b':2} ->>>

Second, the Python garbage collection mechanism:

1, the first Python garbage collection mechanism is based on the reference count, the classification as a supplement.

2, reference count: When there is an object that references the target object, the target object reference count is added one. The reverse operation is the reference count minus one, and the object is deleted when the reference count is 0.

3. Class object and Basic data type Object (destructor) __del__:

1 classA:2     def __init__(self):3         Print "Create Object"4     def __del__(self):5         Print "Delete Object"6 7 #a = a ()8 #del a9 #Other types:Ten #A = ten One #B = "SSSs" A #del a - #del b

4, reference count +1 situation:

(1) objects are created, such asa=23

(2) The object is referenced, for exampleb=a

(3) The object is passed into a function as a parameter, for examplefunc(a)

(4) An object is stored in a container as an element, for examplelist1=[a,a]

5, reference count-1 of the situation:

(1) An alias for an object is explicitly destroyed, such asdel a

(2) The alias of the object is assigned to a new object, such asa=24

(3) An object leaves its scope, such as the local variable in the Func function when the F function finishes executing (global variable does not)

(4) The container where the object is located is destroyed, or the object is deleted from the container

6. Indicate:

Function call variable A,a reference count +2 because there is one more step to the argument.

7. GC Module

1 #referencing Lib files2 ImportGc#GC module File is_enable () =true does not start garbage collection automatically3 ImportSYS4 5Gc.set_debug (GC. DEBUG_STATS|GC. Debug_leak)#Open the GC module debug Information6 7 #gets the reference count of the target object8A = []9b =aTen PrintSys.getrefcount (a) One PrintSys.getrefcount (b) A  -Gc.collect (para)#returns the number of unreachable objects. The reference count for the loop application is not 0, but it is also garbage, and the revenue is gc.garbage but not deleted.  - #para = 0,1,2 0 check the first generation object 1 Check one or two generation object 2 check one or two, three generations of objects.  the  -Gc.set_threshold (Threshold0[,threshold1[,threshold2])#How often garbage collection is performed automatically -Gc.get_count ()#gets the current auto-performing garbage collection counter, a list of length 3 -  + #garbage collection has a threshold assumption threshold (700,10,10) that will start when it is (699+1,x,x). 

8. References (acknowledgement):

Http://www.cnblogs.com/Xjng/p/5128269.html

http://blog.csdn.net/yueguanghaidao/article/details/11274737

Http://www.cnblogs.com/CBDoctor/p/3781078.html

Python's memory management and garbage collection mechanism learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.