search
HomeDatabaseRedisHow to clean up Redis memory fragments

What is Redis memory fragmentation?

The total amount of remaining space in the operating system is sufficient, but when applying for a space with a continuous address of N bytes, there is no continuous space of N bytes in the remaining memory space. Then, among the remaining memory spaces, less than N The contiguous memory space of bytes is memory fragmentation.

How is Redis memory fragmentation formed?

There are internal and external reasons for the formation of memory fragmentation:

  • Internal reason: The allocation strategy of the memory allocator determines that the operating system cannot achieve "allocation on demand".

    • Redis uses libc, jemalloc, and tcmalloc to allocate memory. By default, jemalloc is used.

    • The memory allocator allocates memory space according to a fixed size, not entirely according to the memory size requested by the application.

    • Taking jemalloc as an example, it divides the memory space according to a series of fixed sizes, such as 8 bytes, 16 bytes, 32 bytes,..., 2KB, 4KB, etc. Jemalloc will allocate a fixed size space closest to the memory requested by the program.

  • External reason: The key-value pairs are of different sizes, and the key-value pairs can be modified and deleted.

    • When Redis applies for memory space allocation, for memory space requirements of different sizes, the memory allocator allocates memory space according to a fixed size. The allocated memory space is generally larger than the requested memory space. The memory space is larger, which will produce certain memory fragmentation.

    • Key-value pairs will be modified and deleted, which will lead to space expansion and release.

How to determine whether Redis has memory fragmentation?

DAS queries the detailed information of memory usage through the INFO command provided by Redis. The command is as follows:

INFO memory
# Memory
used_memory:350458970752
used_memory_human:326.39G
used_memory_rss:349066919936
used_memory_rss_human:325.09G
…
mem_fragmentation_ratio:1.00
  • used_memory: Indicates that Redis is used to save data. The actual memory space requested.

  • used_memory_rss: Indicates the physical memory space actually allocated to Redis by the operating system, which includes memory space fragments.

  • mem_fragmentation_ratio refers to the current memory fragmentation rate of Redis. Calculation formula: mem_fragmentation_ratio=used_memory_rss/used_memory

    • mem_fragmentation_ratio is greater than or equal to 1 but less than or equal to 1.5. This situation is reasonable.

    • mem_fragmentation_ratio is greater than 1.5, indicating that the memory fragmentation rate has exceeded 50%.

How to clean up memory fragments?

A "simple and crude" method is to restart the Redis instance. However, this method will bring two consequences:

  • If the data in Redis is not persisted, the data will be lost;

  • Regardless of the Redis data Whether it is persisted or not, the AOF or RDB method needs to be used to restore the data, and the recovery time depends on the size of the AOF or RDB file. And if there is only one Redis instance, services cannot be provided during the recovery phase.

Is there a better way? Yes, from version 4.0-RC3 onwards, Redis itself provides a method to automatically clean up memory fragments.

Automatic cleaning of memory fragments

Cleaning of memory fragments, simply put, means "moving to make way and merging space".

When there is data that divides a continuous memory space into several discontinuous spaces, the operating system will copy the data to another place, and the original discontinuous memory space becomes a continuous memory space. .

But fragmentation cleanup comes at a cost. Moving multiple copies of data to a new location and freeing up the original space is something the operating system must do, but this process takes time. In addition, when data is copied, Redis will be blocked and performance will be reduced.

How to alleviate this problem?

Redis specifically provides parameter settings for the automatic memory fragmentation cleaning mechanism. You can set parameters to control the start and end timing of fragmentation cleaning, as well as the proportion of CPU occupied, thereby reducing the performance impact of fragmentation cleaning on Redis request processing.

First, turn on automatic memory fragmentation cleanup:

config set activedefrag yes
Then, set the conditions that trigger memory cleanup:

  • active- defrag-ignore-bytes 100mb: Indicates that when the number of bytes of memory fragmentation reaches 100MB, cleanup will begin;

  • active-defrag-threshold-lower 10: Indicates that the memory fragmentation space occupies the operating system allocation When the total space allocated to Redis reaches 10%, cleanup begins.

Finally, control the upper and lower limits of the proportion of CPU time occupied by cleaning operations:

  • active-defrag-cycle-min 25: Indicates automatic cleaning The proportion of CPU time used in the process is not less than 25% to ensure that the cleaning can proceed normally;

  • active-defrag-cycle-max 75: Indicates that the proportion of CPU time used in the automatic cleaning process is not high Above 75%, once it exceeds, the cleaning will be stopped to avoid a large number of memory copies blocking Redis during cleaning, resulting in increased response latency.

The above is the detailed content of How to clean up Redis memory fragments. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

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

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),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use