PHP7 Kernel Analysis 9 Memory Management
The content of this article is about the memory management of PHP7 kernel analysis 9. Now I share it with you. Friends in need can refer to it
1.Zend memory pool
The memory pool is the lowest-level memory operation in the kernel. It defines three granular memory blocks: chunk, page, and slot. The size of each chunk is 2M, the page size is 4KB, and a chunk is cut into 512 pages, and one or several pages are cut into multiple slots, so when applying for memory, the specific allocation strategy is determined according to different application sizes:
Huge(chunk): 申请内存大于2M,直接调用系统分配,分配若干个chunk Large(page): 申请内存大于3K(3/4 page_size),小于2044K(511 page_size),分配若干个page Small(slot): 申请内存小于等于3K(3/4 page_size)
2.zend heap structure
chunk consists of 512 pages, of which the first page is used to save the chunk structure, and the remaining 511 pages are used for memory allocation. Page is mainly used for the allocation of Large and Small memory. Heap is a structure that represents the memory pool. It is the most important structure and is used to manage the allocation of the above three types of memory. In Zend There is only one heap structure.
3. Memory allocation
Huge allocation
Application for more than 2M memory, and general memory application There is not much difference, except that the requested memory blocks are managed through a singly linked list. Huge allocation actually means allocating multiple chunks. Chunk allocation is also the basis for large and small memory allocation. It is the only granularity that ZendMM can apply for memory from the system. There is a key operation when applying for chunk memory, which is to align the memory address to ZEND_MM_CHUNK_SIZE, which means that the applied chunk addresses are all integer multiples of ZEND_MM_CHUNK_SIZE
Large allocation
is greater than 3/4 Page_size (4KB) and less than or equal to 511 page_size memory applications, that is, the size of a chunk is enough (the reason why it is 511 pages instead of 512 is because the first page is always occupied by the chunk structure), if you apply for more If there are pages, these pages will be consecutive when allocated. If the last chunk is not found, a new chunk will be reallocated and inserted into the chunk linked list.chunk->free_map uses bitmap to record the usage of each group of pages
a.首先会直接跳过group1,直接到group2检索 b.在group2中找到第一个可用page位置:67,然后向下找第一个不可用page位置:69,找到的可用内存块长度为2,小于3,表示此内存块不可用 c.接着再次在group2中查找到第一个可用page位置:71,然后向下找到第一个不可用page位置:75,内存块长度为4,大于3,表示找到一个符合的位置,虽然已经找到可用内存块但并不"完美",先将这个并不完美的page_num及len保存到best、best_len,如果后面没有比它更完美的就用它了 d.再次检索,发现group2已无可用page,进入group3,找到可用内存位置:page 130-132,大小比c中找到的合适,所以最终返回的page就是130-132 e.page分配完成后会将free_map对应整数的bit位从page_num至(page_num+page_count)置为1
Small allocation
Small memory refers to memory smaller than (3/4 page_size). These memories first apply for 1 or more pages. Then these pages are cut to a fixed size, so the first step is exactly the same as the previous section Large allocation. There are a total of 30 fixed size specifications of small memory: 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128... 1792, 2048, 2560, 3072 Byte, we call this slot, The size of these slots is regular: the smallest slot size is 8byte, the first 8 slots are incremented by 8byte, and every 4 subsequent increments are multiplied by 2
step1: 首先根据申请内存的大小在heap->free_slot中找到对应的slot规格bin_num,如果当前slot为空则首先分配对应的page,free_slot[bin_num]始终指向第一个可用的slot step2: 如果申请内存大小对应的的slot链表不为空则直接返回free_slot[bin_num],然后将free_slot[bin_num]指向下一个空闲位置 step3: 释放内存时先将此内存的next_free_slot指向free_slot[bin_num],然后将free_slot[bin_num]指向释放的内存,也就是将释放的内存插到链表头部
Related recommendations:
PHP7 Kernel Analysis 8 and the like
PHP7 Kernel Analysis 7 Zend Engine Execution Process
PHP7 Kernel Analysis 6 Functions
The above is the detailed content of PHP7 Kernel Analysis 9 Memory Management. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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,

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.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

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.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
