PHP Principles of memory management difficult to understand a few points _php tutorial

Source: Internet
Author: User
PHP memory management, divided into two parts, the first part is the memory management of PHP itself, this part of the main content is the reference count, write-time replication, and so on application-oriented aspects of management. And the second part is today I want to introduce, zend_alloc in the description of PHP's own memory management, including how it manages the available memory, how to allocate memory and so on.

In addition, why write this, because there is no information on the use of PHP memory management strategy, data structure, or algorithms. While we usually develop extensions, fix PHP bug, but this part of the knowledge need to have a good understanding. Many friends in the PHP development group are not very clear about this piece, so I think it is necessary to write a special.

Some basic concepts, I will not repeat, because look at the code is easy to understand, I am here to introduce a few look at the code is not so easy to understand the point, why so say, hehe, I wrote the article before, found the following existing information, has avoided duplication of work, which saw the Tipi project on this part of the description, Found a lot of errors. So, I think that's part of the point of looking at code that's not that easy to understand.

At present, the English version of the introduction is also in writing: Zend MM

Zend Memory Manager, hereinafter referred to as Zend MM, is the logic of RAM management in PHP. One of the key data structures: Zend_mm_heap:

498) this.width=498; ' OnMouseWheel = ' javascript:return big (This) ' style= ' width:427px; height:308px "alt=" \ "src=" Http://www.bkjia.com/uploadfile/2013/0904/20130904095402180.png "width=" 597 "height=" 451 "/>

Zend mm memory is not for small chunks of memory and large chunks of memory, the difference, for small pieces of memory, this part is the most commonly used, so the pursuit of high performance. For large chunks of memory, the pursuit is safe, try to avoid memory waste.

So, for small chunks of memory, PHP also introduces the cache mechanism:

498) this.width=498; ' OnMouseWheel = ' javascript:return big (This) ' style= ' width:416px; height:264px "alt=" \ "src=" Http://www.bkjia.com/uploadfile/2013/0904/20130904095403722.png "width=" 521 "height=" 385 "/>

Zend MM hope to do as far as possible through the cache, one location can find the allocation.

And a point that is not easy to understand is Free_buckets's statement:

Q: Why is the length of the free_buckets array zend_mm_number_bucket?

A: This is because PHP uses a technique here, using a fixed-length array to store Zend_mm_number_bucket Zend_mm_free_block, as shown in the red box. For a free_buckets element that is not used, the only useful data structure is next_free_block and prev_free_block, so in order to save memory, PHP does not assign Zend_mm_number_bucket * sizeof (Zend_mm_free_block) size of memory, but only with Zend_mm_number_bucket * (sizeof (*next_free_block) + sizeof (*prev_free_block)) Size of Memory:

Let's look at the definition of Zend_mm_small_free_bucket macro:

 
  
  
  1. #define Zend_mm_small_free_bucket (heap, index)
  2. ( zend_mm_free_block*) ((char*) &heap->free_buckets[index * 2] +
  3. sizeof (zend_mm_free_block*) * 2-

After that, Zend MM guarantees that only the prev and next pointers are used, so there is no memory read error.

Then, the second point is not easy to understand, that is, PHP management of Large_free_buckets, first introduce the allocation (Tipi project group of this part of the description is somewhat ambiguous):

 
  
  

Large_free_buckets can be said to be a combination of achievements and two-way lists:

498) this.width=498; ' OnMouseWheel = ' javascript:return big (This) ' style= ' width:413px; height:263px "alt=" \ "src=" Http://www.bkjia.com/uploadfile/2013/0904/20130904095403111.png "width=" 555 "height=" 351 "/>

Large_free_buckets uses a macro to determine the size of a memory, on what index:

 
  
  

Zend_mm_high_bit gets the ordinal number of the highest 1 in true_size (zend_mm_high_bit), the corresponding assembly instruction is BSR (here, the description of the Tipi project error is: "This hash function is used to calculate the number of bits of size, The return value is the number of 1 in size binary -1″).

That is, each element in the Large_free_buckets maintains a pointer to a memory block of size that is 1 at the corresponding index. Eh, a bit of a detour, for example:

For example, for large_free_buckets[2], only the memory size in 0b1000 to 0b1111 size is saved. Again, for example: large_free_buckets[6], a pointer to a memory size of 0b10000000 to 0b11111111 is saved.

In this way, when allocating memory, Zend mm can be quickly located to the most likely suitable area to find. Improve performance.

And, each element is also a two-way list, maintaining the same size of the memory block, and the Left and right children (child[0] and child[1] respectively represents the key values 0 and 1, what is the key value?

Let's give an example, for example, I applied to php a true_size for 0b11010 size of memory, after some steps, did not find the appropriate memory, PHP entered the zend_mm_search_large_block logic to Large_free_ Find the right memory in the buckets:

1. First, calculate the true_size corresponding to the INDEX, the calculation method as described earlier Zend_mm_large_bucket_index

2. Then in a bitmap structure, determine if there is a greater than true_size of available memory already exists in the large_free_buckets, if not exist to return:

 
  
  
  1. size_t bitmap = Heap->large_free_bitmap >> index;
  2. if (bitmap = = 0) {
  3. return NULL;

3. Determine if there is memory available for Free_buckets[index]:

 
  
  
  1. If

4. If present, start with Free_buckets[index] and look for the most appropriate memory, steps as follows:

4.1. Starting with Free_buckets[index], if the current memory size of Free_buckets[index] is equal to true_size, look for the end and return successfully.

4.2. View the current highest position of the True_size after index (true_size << (Zend_mm_num_buckets-index)), if 1. Continue to look under free_buckets[index]->child[1], if free_buckets[index]->child[1] does not exist, then jump out. If the current maximum of true_size is 0, continue looking under free_buckets[index]->child[0], if free_buckets[index]->child[0] does not exist, then in Free_ BUCKETS[INDEX]->CHILD[1] below to find the minimum memory (because at this time, the memory under free_buckets[index]->child[1] is greater than true_size)

4.3. Change the starting point to the child as described in 2 and move left one ture_size.

5. If the above logic does not find the appropriate memory, look for the smallest "chunk memory":

 
  
  
  1. /* Search for smallest ' large ' free block */
  2. Best_fit = p = heap->large_free_buckets[index + zend_mm_low_bit (bitmap)];
  3. while ((p = p->child[p->child[0]! = NULL])) {
  4. if (Zend_mm_free_block_size (P) < zend_mm_free_block_size (Best_fit)) {
  5. Best_fit = p;
  6. }

Note The above logic, (P = p->child[p->child[0]! = NULL]), PHP is trying to find the smallest memory possible.

Why say, Large_free_buckets is a key tree, from the above logic we can see, PHP to a size, in accordance with the binary 0,1 do key, the memory size information to the key tree, convenient to quickly find.

In addition, there is a rest_buckets, this structure is a two-way list, to save some of the rest of the PHP allocation of memory, to avoid meaningless to insert the remaining memory Free_buckets performance problems (here, tipi project error Description: " This is an array of only two elements. Our common insert and find operations are for the first element, Heap->rest_buckets[0] ").

Author: laruence () original address:

http://www.bkjia.com/PHPjc/445706.html www.bkjia.com true http://www.bkjia.com/PHPjc/445706.html techarticle PHP memory management, divided into two parts, the first part is the memory management of PHP itself, this part of the main content is the reference count, write-time replication, and so on the application-oriented level of management. and ...

  • Related Article

    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.