search
HomeBackend DevelopmentPHP TutorialPHP kernel--detailed graphic explanation of memory management and caching mechanism

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;
?>

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
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
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function