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!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
