python-Memory Management

Source: Internet
Author: User

Python memory management


Python's memory management consists of three mechanisms: the reference counting mechanism, the garbage collection mechanism, and the memory pool mechanism.

1. Reference counting mechanism

Brief introduction
Python internally uses a reference count 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, creates a reference count, and when the object is no longer needed, the object's reference count is 0 o'clock, and it is garbage collected.

Characteristics
1. When assigning a new name to an object or putting an object into a container (list, tuple, or dictionary), the object's reference count increases.

2. When you use Del to display destruction on an object or if the reference is out of action or is re-assigned, the reference count of the object is reduced.

3. You can use the Sys.getrefcount () function to get the current reference count of an object. In most cases, the reference count is much larger than we guessed.
For immutable data (numbers and strings), the interpreter shares memory in different parts of the program to conserve memory.

2. Garbage collection mechanism

Characteristics
1. When there are parts in memory that are no longer in use, the garbage collector cleans them out. 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. The garbage collection mechanism also has 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).

3. Memory pool mechanism

Brief introduction
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 the memory of the application is not to create objects,
Therefore, 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 the user state and the kernel mentality,
This will seriously affect the execution efficiency of Python. 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 Concepts
The concept of a memory pool is to pre-request a certain amount of memory in memory, the size of an equal number of blocks for backup, when there is a new memory requirements, first allocate memory from the memory pool to this demand,
Not enough, then apply for new memory. The most significant advantage of this is the ability to reduce memory fragmentation and increase efficiency. Memory pools are implemented in many ways, with different performance and scope of application.

Characteristics
1.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.

2.Pymalloc mechanism. To speed up the execution of Python, Python introduces a memory pooling mechanism for managing the application and release of small chunks of memory.

All objects less than 256 bytes in 3.Python use the allocator implemented by Pymalloc, while large objects use the system malloc.

4. For Python objects, such as integers, floating-point numbers, and lists, have their own private pools of memory that are not shared between objects. Which means if you allocate and release a lot of integers,
The memory used to cache these integers can no longer be assigned to floating-point numbers.

Note:

The small data pool of int, the number range does not exceed -5~256, the data does not exceed this range during the assignment, the memory address is not changed.

STR's small data pool, the multiplication of str s * 20 (including special characters and numbers), or at the same address, s*21 is a two memory address

python-Memory Management

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.