Home > Backend Development > PHP Tutorial > PHP7 Kernel Analysis 9 Memory Management

PHP7 Kernel Analysis 9 Memory Management

不言
Release: 2023-03-23 18:12:02
Original
1757 people have browsed it

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)
Copy after login


2.zend heap structure

PHP7 Kernel Analysis 9 Memory Management

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

PHP7 Kernel Analysis 9 Memory Management

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
Copy after login

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

PHP7 Kernel Analysis 9 Memory Management

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]指向释放的内存,也就是将释放的内存插到链表头部
Copy after login

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!

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