PHP kernel--probe into the graphics and text of memory management and caching mechanism

Source: Internet
Author: User
Preface:

The memory that PHP needs to run is a one-time application to the operating system, not multiple times. So how does he apply one-off, and how is the mechanism? Take a look at the introduction below.

heap layer is the core implementation of PHP memory management , PHP underlying memory management, ZENDMM to the system memory application, not when necessary to the system to apply immediately, but by the zendmm of the bottom (heap layer) to the system first request a large chunk of memory, establish a similar memory pool management mechanism, unset, ZENDMM does not directly return the memory to the system immediately, but only in its own maintenance Memory Pool (storge layer) re-identify it as available ,.

Advantages:

1. the predefined constant volume is many, the memory request has hundreds of times, avoids the PHP to the system frequent memory request operation, reduces the request to the OS the number of times.

2. The operation speed is faster, the disadvantage is that as the program runs longer, memory usage is more and more, so 5.3 introduces a new garbage collection mechanism.

Detailed analysis is as follows:

The memory management of PHP can be considered as layered (hierarchical). It is divided into three tiers: the storage layer (storage), the heap layer (heap), and the interface layer (Emalloc/efree). The storage layer actually applies memory to the system through functions such as malloc (), mmap (), and frees the requested memory through the free () function.



The storage layer usually requests the memory block is larger, here the request memory large does not refer to the storage layer structure needs the memory big, just heap layer through calls the storage layer's allocation method, it requests the memory in the big Chunk way, the storage layer's function is the memory allocates the way to the heap layer to be transparent.

As shown below, the PHP memory manager. PHP has 4 memory allocation schemes on the storage tier: Malloc,win32,mmap_anon,mmap_zero, which uses malloc to allocate memory by default, or Windows version if ZEND_win32 macros are set. Call HeapAlloc to allocate memory, the remaining two memory schemes are anonymous memory mappings, and the PHP memory scheme can be modified by setting environment variables.



Figure 6.1 PHP Memory Manager


I. Application of memory

heap layer is the core implementation of PHP memory management , PHP underlying memory management, around the small block of Memory list (free_buckets), chunk memory list (large_free_buckets) and the remaining memory list (Rest_ Buckets) Three lists to be layered. Zend MM to the system memory application, not when necessary to the system to apply immediately, but by the Zendmm of the bottom (heap layer) to the system first request a large chunk of memory, through the above three kinds of list of filling, establish a similar to the memory pool management mechanism.


Zendmm allocates the appropriate memory in the memory pool for use when the program is running with memory. the benefit of this is to avoid PHP's frequent memory requests to the system , such as the following code:

<?php  $tipi = "o_o\n";  Echo $tipi;? >

This is a simple PHP program, but through the Emalloc call count, just PHP program, only a variable is assigned, but found that the memory of the request hundreds of times, of course, this is very easy to explain, because the execution of PHP script requires a large number of environment variables and internal variable definition (see details PHP Kernel-the life cycle ), these definitions themselves need to be stored in memory.

When writing PHP extensions, it is recommended to use the Emalloc (Application is Zend_mm_storage layer of memory block) to replace malloc(application is the memory block of the operating system) , in fact, the use of PHP Zendmm instead of manual direct call system-level memory management.


Zend MM Use _Zend_mm_alloc_int function for memory allocation, the flow is as follows:




from the above allocation can be seen, PHP allocation of memory, is the use of PHP for the purpose of design, PHP is generally used for Web application data support, a single script running cycle is generally relatively short (up to the second level), the application of large chunks of memory, the allocation of autonomous small pieces, Instead of a more complex, non-pro-address free memory merge, the focus is on the system request again. The advantage of this is that it will run faster, with the disadvantage that memory usage will be "more and more" (PHP5.2 and earlier) as the program runs longer. So the previous version of PHP5.3 is not intended to be a long-running daemon. (Of course, there are other ways to solve this, and a new GC mechanism is introduced in PHP5.3, as described in the next section of the PHP kernel-memory leaks and new garbage collection mechanisms )


Two. Destruction of memory

Zend MM in memory destruction processing with the same policy as the memory application, when the program unset a variable or other release behavior,Zendmm will not directly immediately return the memory to the system, but only in their own maintenance The memory pool (storge layer) is re-identified as available and is organized into the three lists (Small,large,free) described above for use in the next memory request.


zend _mm_small_size and the cache has not exceeded the system settings zend _mm_cache_size, the current memory block zend _mm_block will be put back into Mm_heap->cache.

In the memory destruction process, reference counts and garbage collection (GC) are also involved, which are discussed in the sections that are behind. See also PHP kernel--memory leak and new garbage collection mechanism


Three. Caching

In Wikipedia, there is a description: anything in the speed difference between the two hardware, to coordinate the data transmission speed differences between the structure, can be called the cache. starting with the cache between the processor and the memory, it is all about adapting the speed of the data access to the CPU's processing speed, based on the principle of "local sexual behavior of program execution and data access" in memory. Similarly, the cache in PHP memory management is based on the principle of "local sexual behavior of program execution and data access". The introduction of caching is to reduce the number of queries for small chunks of memory (see if the cache can be hit before querying), providing faster access to recently accessed data.

PHP adds the cache to the memory management mechanism by doing some of the following:

• Identify cache and cache size limits, that is, when caching is used, and in some cases, disable caching with minimal modification

• Cached storage structure, which is the location, structure, and storage logic of the cache

• Initializing the cache

• Get content in cache

• Write Cache

Release the cache or empty the cache list

The cache itself is stored in the memory requested by the storage layer, and if the memory is not enough, then the cache must be released.

When the heap memory overflows, the program calls Zend_mm_free_cache to release the cache. The entire release process is a traversal array, for each element of the array the program iterates through the elements in its linked list before itself, performs a merge memory operation, and reduces the cache metering numbers in the heap structure .

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.