Python memory management analysis-Python tutorial

Source: Internet
Author: User
This article mainly introduces python memory management and analyzes the memory management mechanism of Python in detail. if you need it, you can refer to this article to analyze the python memory management mechanism in detail. Share it with you for your reference. The specific analysis is as follows:

Memory management is a crucial part of a dynamic language such as Python. it even determines the execution efficiency of Python to a large extent, because in the running of Python, will create and destroy a large number of objects, these involve memory management.

Memory pool of small block space

In Python, many times the applied memory is small memory. these small memory will be released soon after the application exists. because these memory applications are not used to create objects, therefore, there is no memory pool mechanism at the object level.

Python memory pool Panorama

This means that Python will execute a lot of malloc and free operations during the runtime, and switch between the user state and the core state frequently, which seriously affects the execution efficiency of Python. To accelerate the execution efficiency of Python, Python introduces a memory pool mechanism to manage the application and release of small memory blocks. This is also the Pymalloc mechanism mentioned earlier.

In Python 2.5, the default boundary between small memory and large memory in Python is set to 256 bytes. this boundary point is controlled by the SMALL_REQUEST_THRESHOLD symbol we saw earlier.

That is to say, when the applied memory is less than 256 bytes, PyObject_Malloc will apply for memory in the memory pool; when the applied memory is greater than 256 bytes, PyObject_Malloc will be converted to malloc. Of course, by modifying the Python source code, we can change this default value to change the default memory management behavior of Python.

When the reference count of an object is reduced to 0, the corresponding destructor of the object will be called.

However, it is important to note that calling the destructor does not necessarily mean that free will be called to release the memory space. if so, the frequent application and release of memory space will greatly reduce the execution efficiency of Python (not to mention the fact that Python has been suffering from dissatisfaction with its execution efficiency for many years ). In general, Python uses a large amount of memory object pool technology. using this technology can avoid frequent application and release of memory space. Therefore, during the analysis, the space occupied by objects is usually returned to the memory pool.

"This problem is: The Python arena never releases the pool. Why is this problem similar to memory leakage. In this case, we apply for 10*1024*1024 small 16-byte memory, which means we must use MB of memory, because Python does not open the WITH_MEMORY_LIMITS compilation symbol of the memory pool restricted mentioned above by default, Python will use arena completely to meet your needs, which is no problem, the key problem is that after a while, you have released all the 16-byte memory and the memory has been back to arena control. it seems that there is no problem.

But the problem occurs exactly at this moment. Arena never releases the pool set maintained by it, so the MB of memory is always occupied by Python. if the program runs in the future, it will no longer need such a huge memory of MB, isn't the memory wasted? "

Python memory management rules: when del is used, the list elements are released, and the large objects of the management elements are recycled to the py object buffer pool.

I hope this article will help you with Python programming.

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.