Python introduces a mechanism: reference counting.
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 also a recycle garbage collector, which ensures that the circular reference object is released (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.
How is python managed in memory?