In-depth explanation of Python's value-transfer problem and memory management mechanism

Source: Internet
Author: User
Tags shallow copy

At a relatively shallow level, we have further insight into the Python memory management mechanism by explaining the following questions:

Is python a "pass-through" or "pass-value"?

The answer to this question is: look at the situation. There are some passing values, some are quoted.

The basis of judgment is to see the variability of the object, which depends on the type of object . So the argument in Python is that the object is mutable or immutable.

The basic data types (integer and other data types, strings) and tuples are immutable, the parameters pass the formal parameter, that is, a copy of the original value is passed, and the value arguments in the function that change the parameter do not change:

def func (a)

lists, dictionaries, classes, and class instances are mutable data types, and as parameter passing is a reference to the original value, the function changes the parameter list, and the arguments change accordingly:

def func (a=[])

You can use tuples for parameter passing, because tuples are not allowed to change.

A common example is list.sort (), which is sorted directly on the list rather than returning it, and can be copied by itself: Newlist=list (mylist), or newlist=mylist[:]

The same goes for copying objects (a=b) in Python, where immutable objects are actually copied, and mutable objects simply copy a reference to it.

Example 1:

list0=[1,2, ' A ', [' B ', ' C ']

list1= list0

list1[0]=11

list0[3][0]= ' d '

Print List1 #[11,2, ' a ', [' d ', ' C ']

Print list0 #[11,2, ' a ', [' d ', ' C ']

List0, ' A ' is immutable, [' B ', ' C '] is a mutable object

Example 2: (Shallow copy)

list0=[1,2, ' A ', [' B ', ' C ']

List1=list (LIST0)

list1[0]=11

list0[3][0]= ' d '

Print List1 #[11,2, ' a ', [' d ', ' C ']

Print List0 #[1, 2, ' a ', [' d ', ' C ']

List0, ' A ' is immutable, [' B ', ' C '] is a mutable object


After the above explanation, perhaps we have initially realized which is the reference, which is the value of the, so far, we have not really understood its internal principle:

Consider the following example:

>>a=1

>>b=a

ID (a) ==id (b), at which point B and a refer to the same address, at which point B is also a reference to a

When the value of B changes, it points to another address, that is, reallocating memory

>>b+=1

ID (a)!=id (b)

>>a=[1,2,3]

>>b=a

At this point B is a reference to a. ID (a) ==id (b)

>>b=[4,5,6]

At this point, B re-allocates memory.

Another point of note: when we define a variable with the same name as the function, and then use the system function, Python will prompt is not available, then we want del varible to delete the variable :

such as: >>>list=[1,2,3]

>>>newlist=list (list) #这时外面的list也会被当做列表来使用造成错误

>>>del List


Principle:

Any variable in Python is an object, so the parameter supports only the reference delivery method. That is, by the mechanism of the name binding, the value of the actual parameter and the name of the form parameter are bound together, the formal parameters and the actual parameters point to the same storage space in memory.


Any variable in Python is an object, and an immutable object is a reference to the fact that the value is passed, and the parameters and arguments are not consistent because the immutable object changes are a re-assignment (that is, the process of reallocating memory). The variable object change value is the original memory address based on add or append, is not a re-assignment process, through the above example we can find that the variable object re-assignment address is also changed.

Let's take a closer reading of Python's memory management mechanism:

Python introduces a mechanism: Reference Count. Python internally uses reference counting to keep track of objects in memory, and Python internally records how many references the object has, that is, the reference count, when the object is created, a reference count is created, and when the object is no longer needed, the object has a reference count of 0 o'clock, which is garbage collected.

Summarize the object in the case of a reference count plus 1:

1. Object created: x=4

2. Other people were created: y=x

3. Passed as a parameter to the function: Foo (x)

4. As an element of the container object: a=[1,x, ' 33 ']

Reference count Reduction Scenario

1. A local reference has left its scope. For example, at the end of the foo (x) function above, X points to the object reference minus 1.

2. The alias of the object is explicitly destroyed: Del x; or del y

3. An alias of an object is assigned to another object: x=789

4. Object is removed from a Window object: Mylist.remove (x)

5. The Window object itself is destroyed: Del myList, or the Window object itself is out of scope.


Garbage collection

1. When there are no longer parts in memory, the garbage collector will clean them off. it checks for those objects that have a reference count of 0, and then clears its memory space. Of course, in addition to the reference count of 0 will be cleared, there is also a situation will be cleared by the garbage collector: when two objects refer to each other, their own other references are already 0.

2, garbage collection mechanism there is a Recycle garbage collector, make sure to release the circular Reference object (a reference B, B refers to a, which causes its reference count to never be 0).


In Python, many times the requested memory is small chunks of memory, these small pieces of memory after the application, will soon be released, because these memory applications are not to create objects, so there is no object-level memory pool mechanism. This means that Python performs a large amount of malloc and free operations during runtime, frequently switching between user state and kernel mentality, which can seriously affect Python's execution efficiency. To speed up the execution of Python, Python introduces a memory pooling mechanism for managing the application and release of small chunks of memory.

Memory pool mechanism


Python provides a garbage collection mechanism for memory, but it puts unused memory into the memory pool instead of returning it to the operating system.

All objects less than 256 bytes in Python Use the allocator implemented by Pymalloc, while large objects use the system malloc. In addition, Python objects, such as integers, floating-point numbers, and lists, have their own private pools of memory that do not share their pools of memory among objects. That is, if you allocate and release a large number of integers, the memory used to cache these integers can no longer be assigned to floating-point numbers.


In Python, many times the requested memory is small chunks of memory, these small pieces of memory after the application, will soon be released, because these memory applications are not to create objects, so there is no object-level memory pool mechanism. This means that Python performs a large amount of malloc and free operations during runtime, frequently switching between user state and kernel mentality, which can seriously affect Python's execution efficiency. To speed up the execution of Python, Python introduces a memory pooling mechanism for managing the application and release of small chunks of memory. This is the pymalloc mechanism mentioned earlier.

About the memory management mechanism reference from: http://blog.chinaunix.net/uid-26602509-id-3506965.html


Then we went on to the Python memory and was asked during the interview

Is the list in Python stored as an array or a linked list in memory???

Suddenly was asked to live, think of the beginning to think that is linked to the list, and then feel that it is an array, ambiguous, by looking at some of the data, found that the list of storage is ArrayList (dynamic array), and it corresponds to the LinkedList (linked list), ArrayList is actually an array, in fact, the equivalent of a vector in C + +, but at this point the list can store different types of elements. Then we understand this, use the time will be more attention to insert or delete frequently, the list efficiency is relatively low, after all, it is stored in the form of ArrayList.

What are the tuple storage forms in python?

A tuple is stored in memory as a constant array, because it is an immutable data type, so a constant array is expected to be understood by everyone.

What is the dictionary storage format in python?

Dictionaries are equivalent to maps in the C + + standard library

What about strings?

A string is actually a list that cannot be modified, and it is also the form of an array, which is consistent with C + +.


In-depth explanation of Python's value-transfer problem and memory management mechanism

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.