Home Backend Development PHP Tutorial PHP kernel--detailed graphic explanation of memory management and caching mechanism

PHP kernel--detailed graphic explanation of memory management and caching mechanism

Mar 09, 2017 am 09:47 AM

Foreword:

The memory required by PHP when running is a one-time application to the operating system Opened up, rather than divided into 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 instant 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 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. Instead, ZendMM's bottom layer (heap layer) first applies to the system for a large block. memory, by filling the above three lists, Establish a management mechanism similar to a memory pool.


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
  $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 execution of PHP scripts requires the definition of a large number of environment variables and internal variables (see PHP Kernel--Life Cycle for details), 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 is to use PHP's ZendMM instead Manually call system-level memory management directly.


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, Memory usage will "increase" (PHP5.2 and earlier). 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 in , Organize them 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 cache located between two types of hardware with a large speed difference is used for The structure that coordinates 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. It is based on the principle of "local behavior of program execution and data access" in 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 the 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.


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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles