search
HomeBackend DevelopmentPHP TutorialMemcache memory allocation mechanism

Page (page) is the smallest unit of memory allocation.

Memcached’s memory allocation is in page units. By default, one page is 1M, which can be specified at startup through the -I parameter. If you need to apply for memory, memcached will divide a new page and allocate it to the required slab area. Once a page is allocated, it will not be recycled or reallocated before restarting
Memcache memory allocation mechanism

Slabs (layers) divide the data space

Memcached does not put all sizes of data together,but pre-registers the data The space is divided into a series of slabs, and each slab is only responsible for data storage within a certain range. As shown in the figure below, each slab only stores data that is larger than the size of the previous slab and smaller than or equal to its own maximum size. For example: slab 3 only stores data between 137 and 224 bytes. If a data size is 230byte it will be allocated to slab 4. As can be seen from the figure below, the space responsible for each slab is actually not equal. memcached By default, the maximum value of the next slab is 1.25 times the previous one. This can be modified by Change the -f parameter to modify the growth ratio.
Memcache memory allocation mechanism

Chunk (block) is the unit for storing cached data.

Chunk is a series of fixed memory spaces. This size is the maximum storage size of the slab that manages it. For example: all chunks of slab 1 are 104byte, and all chunks of slab 4 are 280byte. Chunk is where memcached actually stores cached data. Because the chunk size is fixed to the maximum value that the slab can store, all data allocated to the current slab can be stored in the chunk. If the time data size is smaller than the chunk size, the free space will be idle. This is designed to prevent memory fragmentation. For example, in the figure below, chunk The size is 224byte, and the stored data is only 200byte, and the remaining 24byte will be idle.
Memcache memory allocation mechanism

Slab’s memory allocation. Memcached specifies the maximum memory usage through -m when starting,

but this will not be occupied as soon as it is started, and is gradually allocated to each slab as needed.

If a new cache data is to be stored, memcached first selects a suitable slab, and then checks whether there are any free chunks in the slab. If so, it will be stored directly; if not, it will be applied for.

Slab uses page as the unit when applying for memory, so when the first data is put in, no matter what the size is, a 1M page will be allocated to the slab. After applying for a page, slab will divide the memory of this page according to the size of the chunk, so that it becomes an array of chunks, and then select one from the chunk array to store data. As shown below, slab 1 and slab 2 allocate a page and divide it into chunk arrays according to their respective sizes.
Memcache memory allocation mechanism

Memcached memory allocation strategy.

Based on the above introduction, the memory allocation strategy of memcached is: allocate pages according to slab requirements, and each slab uses chunk storage as needed.

There are several features to note here:

    The pages allocated by Memcached will not be recycled or reallocated
  • The memory applied by Memcached will not be released
  • The idle chunks of the slab will not be lent to any other slab.
This article is reproduced from: http://blog.csdn.net/u013927110/article/details/47024499 The above has introduced the Memcache memory allocation mechanism, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.