Home > Backend Development > PHP Tutorial > Exploring the memory management and caching mechanism of PHP kernel

Exploring the memory management and caching mechanism of PHP kernel

不言
Release: 2023-03-24 07:36:01
Original
1592 people have browsed it

The content of this article is about exploring the memory management and caching mechanism of the PHP kernel. It has certain reference value. Now I share it with you. Friends in need can refer to it

Foreword:

The memory required by PHP when running is applied to the operating system once, rather than multiple times. . So how did he apply for it all at once, and what was the mechanism? Please see the introduction below.

The heap layer is the core implementation of PHP memory management , PHP’s underlying memory management, ZendMM provides the system The memory application is not an immediate application to the system when necessary, but the bottom layer of ZendMM (heap layer) first applies to the system for a large block of memory, and establishes a management mechanism similar to a memory pool. After unset, ZendMM will not directly return the memory to the system immediately, but only in the memory pool (storage layer) maintained by itself. Re-identify it as available,.

Advantages:

1.There are many predefined constant variables, and there are hundreds of requests for memory, Avoids PHP's frequent memory application operations from the system and reduces the number of requests to the OS.

2.The running speed will be faster. The disadvantage is that as the running time of the program becomes longer, the memory usage will increase. , so 5.3 introduces a new garbage collection mechanism.


The detailed analysis is as follows:


PHP’s memory management can be regarded as divided into hierarchical. It is divided into three layers: storage layer (storage), heap layer (heap) and interface layer (emalloc/efree). The storage layer actually applies for memory from the system through functions such as malloc() and mmap(), and releases the requested memory through the free() function.



The storage layer usually applies for larger memory blocks. The memory blocks applied for here are not large. It does not mean that the memory required by the storage layer structure is large, but that when the heap layer calls the allocation method of the storage layer, it applies for memory in large chunks. The function is to make the memory allocation method transparent to the heap layer. As shown in the figure below, PHP memory manager. PHP has 4 memory allocation schemes in the storage layer: malloc, win32, mmap_anon, mmap_zero. By default, malloc is used to allocate memory. If the

ZEND
_WIN32 macro is set, it is the windows version and HeapAlloc is called to allocate memory. The remaining The two memory schemes are anonymous memory mapping, and PHP's memory scheme can be modified by setting environment variables.

Figure 6.1 PHP Memory Manager


1. Memory application

The heap layer is the core implementation of PHP memory management. PHP's underlying memory management revolves around the small memory list (free_buckets), the large memory list (large_free_buckets) and the remaining memory list (rest_buckets) three lists to perform hierarchical processing. ZendMM's memory application to the system does not apply to the system immediately when necessary, but the bottom layer of ZendMM (heap layer) first applies to the system for a large block. By filling the above three lists, a management mechanism similar to a memory pool is established.


When the program needs to use memory, ZendMM will allocate the corresponding memory in the memory pool for use. The advantage of this is to avoid PHP's frequent memory application operations from the system, such as the following code:




[php] view plain copy 
<?php  
  $tipi = "o_o\n";  
  echo $tipi;  
?>
Copy after login


## This is a simple PHP program, but by counting the calls to emalloc, it is just a PHP program that only assigns a variable, but it is found that there are hundreds of requests for memory. Of course, this is very easy to explain, because the PHP script The execution requires the definition of a large number of environment variables and internal variables (see PHP Kernel--Life Cycle for details), and these definitions themselves need to be stored in memory.

When writing PHP extensions, recommended to use emalloc (applying for the memory block of the zend_mm_storage layer ) instead of malloc (applying for the memory block of the operating system), in fact, it means using PHP's ZendMM instead of manually directly calling the system-level memory manage.


ZendMM uses the _zend_mm_alloc_int function for memory allocation. The process is as follows:




##As can be seen from the above allocation, PHP has The allocation is designed based on the purpose of PHP. PHP is generally used for data support in web applications. The running cycle of a single script is generally relatively short (up to seconds). Applications for large blocks of memory can be made independently in small blocks. For allocation, there is no more complicated free memory merging of non-adjacent addresses, but a centralized request to the system again. The advantage of this is that it will run faster. The disadvantage is that as the running time of the program becomes longer, the memory usage will be "more and more" (PHP5.2 and earlier versions). Therefore, versions before PHP5.3 are not suitable for long-term operation as daemon processes. (Of course, there are other ways to solve it, and a new GC mechanism was introduced in PHP5.3. For details, see the following section

PHP Kernel--Memory Leak and New Garbage Collection Mechanism)


2. Destruction of memory

ZendMM adopts the same strategy as memory application in processing memory destruction. When the program unsets a variable or performs other release behaviors, ZendMM will not directly return the memory to the system immediately, but only maintain it in the memory pool (storge layer)Re-identify it as available, and organize it into the three lists mentioned above (small, large, free) according to the size of the memory for use in the next memory application.


##The final implementation function of memory destruction is _efree. In _efree, the destruction of memory must first determine whether to put it back into the cache. If the size of the memory meets

ZEND_MM_SMALL_SIZE and the cache has not exceeded the ZEND_MM_CACHE_SIZE set by the system, then the current memory block zend_mm_block will be put back into mm_heap- >In cache.

In the memory destruction process, reference counting and garbage collection (GC) are also involved, which will be discussed in the following sections. SeePHP Kernel--Memory Leak and New Garbage Collection Mechanism


3. Cache


##There is such a description in Wikipedia:

Any location with a large speed difference The structure between two types of hardware used to coordinate the difference in data transmission speed between the two can be called Cache.

Starting from the initial cache between the processor and the memory, it is all to adapt the speed of data access to the processing speed of the CPU. The principle is based on the "local behavior of program execution and data access" in the memory. Similarly, the cache in PHP memory management is also based on the principle of "local behavior of program execution and data access". The cache is introduced to reduce the number of queries for small memory blocks (check whether the cache can be hit before querying), and provide faster access to recently accessed data. PHP adds the cache to the memory management mechanism and does the following operations:

·Identifies the cache and the size limit of the cache, that is, when to use the cache, in some cases The cache can be disabled with minimal modification

·The cache storage structure, that is, the cache storage location, structure and storage logic

· Initialize the cache

·Get the contents of the cache

·Write the cache

Release the cache or Clear cache list

The cache itself is also stored in the memory requested by the storage layer. If the memory is not enough, the cache must be released. When the heap memory overflows, the program will call zend_mm_free_cache to release the cache. The entire release process is an array traversal. For each array element, the program traverses the element before itself in the linked list, performs a merge memory operation, and reduces the cache measurement number in the heap structure.

Related recommendations:

PHP core SAPI exploration example sharing


The above is the detailed content of Exploring the memory management and caching mechanism of PHP kernel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template