python: deep copy and shallow copy, memory management

The difference between deep and shallow copy copy copy objects are, in essence, whether the address is a copy of the original out of the objects and objects, that is, the difference between the value of the copy of the copy or address.

Variable Object: directly in the address of the object within the meaning of the value changed, is still object to point to this address.

Immutable objects : a value at the address pointed to by the object can not be changed, modified if the value of this object it points to the address change.

Deep copy is totally ago with nothing to do, and how the original object change will not affect the current object

Shallow copy of the original object list element changes, then changes the current object, if the current object in the list of elements changed, will also affect the original object.

 

Memory management

python memory management mechanism is a reference to mixing mechanism counter mechanism and garbage collection

Internal python use reference counting to keep track objects in memory, internal Python objects recorded the number of references, namely the reference count, when the object was created to create a reference count, when the object is no longer needed, this object the reference count is zero, it is garbage.

To summarize what objects in the case of the reference count plus 1:

1. The objects are created: x = 4

2. Another others are created: y = x

3 is passed as a parameter to the function: foo (x)

4. A container element as an object: a = [1, x, '33 ']

Where the reference count reduction

1. a local reference to leave its scope. The above example foo (x) at the end of the function, x pointing object references is decremented.

2. alias object is explicitly destroyed: del x; or del y

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

4. Object is removed from a window object: myList.remove (x)

The window object itself is destroyed: del myList, or the window object itself goes out of scope.

Garbage Collection

1, when the memory is no longer used in some of the garbage collector will put them clean out. It will check that the reference count for the object 0, and then clear the space in memory. Of course, in addition to the reference count is zero is cleared, there is a situation will be cleared away the garbage collector: When two objects refer to each other, they themselves have been other references to the number zero.

2, there is a garbage collection cycle garbage collector, to ensure release of circular reference object (a reference b, b references a, resulting in its reference count never 0).

In Python, in many cases small pieces of application memory is 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 memory object level pooling 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 mechanism

Python provides garbage collection of memory, but it will not have the memory into the memory pool rather than returned to the operating system.

Python all objects smaller than 256 bytes are pymalloc implemented using a dispenser, and large objects malloc system is used. In addition Python objects, such as integers, floating point and List, has its separate private memory pools, between objects do not share their memory pool. This means that if you assign and release a large number of integer, integer memory for caching these can not be allocated to float.

 

Guess you like

Origin www.cnblogs.com/xiao-longxia/p/12607870.html