PHP memory management principle of incomprehensible
PHP memory management principle of incomprehensible several points PHP memory management is divided into two parts, the first part is the PHP's own memory management, which is part of the content is the reference count, copy-on-write, and so on application-oriented level management, while the second part is today I want to introduce, zend_alloc depicted on PHP's own memory management, including how it is to manage the available memory, how to allocate memory.
Also, why write this, it is because there is no information before and to introduce policies PHP memory management used, data structures, or algorithm. And we usually develop extensions, the PHP bug fixes when they knowledge of this part need to have a good understanding of the many friends within the PHP development team is also not very clear on this, so I feel the need to specifically write about.
Some basic concepts, I will not go into details, because it is easy to look at the code can understand, I am here to look at the code introduces a few not so easy to understand the point, why do you say, Oh, before I wrote the article, find the next available information, to avoid duplication of work already, which saw the TIPI this part of the project description and found a lot of mistakes, so I think this part is not so easy to look at the code to understand the point
Currently, the presentation is also written in English: Zend MM
Zend Memory Manager, hereinafter referred to as Zend MM, in PHP memory management logic which has a key data structure:. Zend_mm_heap:
Zend MM put the memory not for small chunks of memory and two memory
types, distinction, for small memory, this part is the most commonly used, so
the pursuit of high performance. For large chunks of memory, the pursuit of
sound, as far as possible avoid memory waste.
So, for small memory, PHP also introduces cache mechanism:
Zend MM hope that through cache as far as possible, be able to find a
positioning assignment.
And a point is not easy to understand free_buckets affirm:
Q: Why is the length of the array is ZEND_MM_NUMBER_BUCKET a free_buckets?
A: This is because, PHP at this point using a technique using a fixed-length array to store ZEND_MM_NUMBER_BUCKET a zend_mm_free_block, as shown in the figure above the red box for a free_buckets element is not used, the only useful data structure. is next_free_block and prev_free_block, therefore, in order to save memory, PHP did not assign ZEND_MM_NUMBER_BUCKET * sizeof (zend_mm_free_block) the size of the memory, but only with the ZEND_MM_NUMBER_BUCKET * (sizeof (* next_free_block) + sizeof (* prev_free_block)) the size of the memory ..
We look ZEND_MM_SMALL_FREE_BUCKET macro definitions:
So, the second point is not easy to understand, allphpfaq.com is that PHP management large_free_buckets, first introduced distribution (TIPI described this part of the project team some ambiguity): static zend_mm_free_block * zend_mm_search_large_block (zend_mm_heap * heap, size_t true_size) large_free_buckets can be said to be a combination of achievements and bidirectional list: large_free_buckets use a macro to determine the size of a memory, what falls on the index: in other words, each element in the large_free_buckets, have maintained a size in point the corresponding index at the size of the memory block pointer 1 eh, a little convoluted, for example: for example, for large_free_buckets [2], it will only save the size 0b1000 to 0b1111 sized memory another example:. large_free_buckets [6 ], it holds the size 0b10000000 to 0b11111111 sized memory pointer. Thus, when the memory redistribution, Zend MM you can quickly locate the most likely area to find appropriate. improve performance while, at the same time and each element is a two-way list, maintained the same size memory block, and about children (child [0] and child [1]), respectively, representing 0 and 1 keys, the key is what do you mean? Let's give an example, such as I would like to apply for a PHP true_size as 0b11010 memory sizes after some steps, did not find a suitable memory, PHP entered zend_mm_search_large_block logic to find the right memory large_free_buckets in: 1. first, calculate true_size corresponding index, calculated as previously described method ZEND_MM_LARGE_BUCKET_INDEX2 then a bitmap structure, it is determined whether there is a greater than true_size available memory already exists in large_free_buckets, if there is no return:. size_t bitmap = heap-> large_free_bitmap >> index; if (bitmap = = 0) {return NULL;} 3 judgment, free_buckets [index] if there is an available memory:.. if (! UNEXPECTED ((bitmap & 1) = 0)) 4, if present, from free_buckets [index] start looking the most suitable memory, as follows:... 4.1 from free_buckets [index] to start if free_buckets [index] and the current memory size true_size equal, find the end of a successful return to the corresponding index after 4.2 View true_size (true_size << (ZEND_MM_NUM_BUCKETS - index)) of the current highest level, if it is 1 in the free_buckets [index] -> child [1] the following continue to look, if free_buckets [index] -> child [1] does not exist, if true_size out of the current. the highest bit is 0, the free_buckets [index] -> child [0] continue to look for the following, if free_buckets [index] -> child [0] does not exist, in free_buckets [index] -> child [1] find below the minimum memory (because at this time can be guaranteed in free_buckets [index] -> child [1] the following are the memory of more than true_size). 4.3 changed the starting point in the child 2, best answer for php the left one ture_size 5. If the above logic does not find a suitable memory, find the smallest & Why, large_free_buckets tree it is a key, from the above we can see that logic, PHP to a size, in binary 0,1 do key, the memory size information reaction the key to the tree, to facilitate quick lookup. in addition, there is a rest_buckets, this structure is a two-way list, to save some post PHP allocate enough memory to avoid meaningless remaining memory inserted free_buckets performance issues ( here, TIPI project description of the error as:
Post Your Ad Here
Comments