What is active defragmentation?
什么是主动内存碎片整理?
Active (online) defragmentation allows a Redis server to compact the spaces left between small allocations and deallocations of data in memory, thus allowing to reclaim back memory.
主动内存碎片整理允许一个Redis服务器在内存中的数据分配和释放过程中对产生的空间进行压缩,以便释放内存。
Fragmentation is a natural process that happens with every allocator (but less so with Jemalloc, fortunately) and certain workloads. Normally a server restart is needed in order to lower the fragmentation, or at least to flush away all the data and create it again. However thanks to this feature implemented by Oran Agra for Redis 4.0 this process can happen at runtime in a “hot” way, while the server is running.
所有分配器在一定负载下都会自然的发生碎片整理(Jemalloc 很少)。 通常,需要重启服务器以降低碎片,或至少清除所有数据然后重新创建它们。但是,由于Oran Agra在Redis 4.0中实现了这个特性,碎片整理过程可以在服务器运行时进行。
Basically when the fragmentation is over a certain level (see the
configuration options below) Redis will start to create new copies of the values in contiguous memory regions by exploiting certain specific Jemalloc features (in order to understand if an allocation is causing fragmentation and to allocate it in a better place), and at the same time, will release the old copies of the data. This process, repeated incrementally for all the keys
will cause the fragmentation to drop back to normal values.
基本上,当碎片达到一定水平(参见下面的配置选项),Redis将通过Jemalloc的功能(以了解分配是否导致碎片,并为它们分配更好的位置)并开始在临近的内存中创建新的这些值的副本,同时也将释放旧的值的副本。这个过程将重复地递增地对所有的键进行,直到碎片整理回到正常值。
Important things to understand:
-
This feature is disabled by default, and only works if you compiled Redis to use the copy of Jemalloc we ship with the source code of Redis. This is the default with Linux builds.
这个功能默认是关闭的,而且只有在你用我们发布的带Jemalloc代码的Redis源码来编译Redis时才能使用这个功能,Linux发布版默认是带这个功能的。
-
You never need to enable this feature if you don’t have fragmentation issues.
如果没有碎片整理问题,你不需要启用这个功能。
-
Once you experience fragmentation, you can enable this feature when
needed with the command “CONFIG SET activedefrag yes”.你可以通过"CONFIG SET activedefrag yes"启用这个功能。
The configuration parameters are able to fine tune the behavior of the defragmentation process. If you are not sure about what they mean it is a good idea to leave the defaults untouched.
配置参数可以调整碎片整理的效果,但如果你不确定这些配置参数的含义,那么你可以让它保持默认值。
# Active defragmentation is disabled by default
# 在默认情况下,主动内存碎片整理是关闭的
# activedefrag no
# Minimum amount of fragmentation waste to start active defrag
# 开始主动内存碎片整理的最小碎片损耗量
# active-defrag-ignore-bytes 100mb
# Minimum percentage of fragmentation to start active defrag
# 开始主动内存碎片整理的最小碎片百分比
# active-defrag-threshold-lower 10
# Maximum percentage of fragmentation at which we use maximum effort
# 最大力度进行碎片整理的最大碎片率百分比
# active-defrag-threshold-upper 100
# Minimal effort for defrag in CPU percentage, to be used when the lower
# threshold is reached
# 碎片整理占用CPU下限阈值
# active-defrag-cycle-min 1
# Maximal effort for defrag in CPU percentage, to be used when the upper
# threshold is reached
# 碎片整理占用CPU上限阈值
# active-defrag-cycle-max 25
# Maximum number of set/hash/zset/list fields that will be processed from
# the main dictionary scan
# set/hash/zset/list 类型的扫描最大元素数量
# active-defrag-max-scan-fields 1000
测试碎片整理
使用配置创建redis实例
redis-standalone.conf
port 7007
bind 0.0.0.0
daemonize yes
logfile "/opt/cachecloud/logs/redis-standalone-7007.log"
dir "/opt/cachecloud/data"
dbfilename "dump_7007.rdb"
# 配置开启主动碎片整理
activedefrag yes
active-defrag-ignore-bytes 10mb
active-defrag-threshold-lower 2
active-defrag-threshold-upper 150
active-defrag-cycle-min 1
active-defrag-cycle-max 25
active-defrag-max-scan-fields 1000
启动redis实例
redis-server /opt/cachecloud/conf/redis-standalone.conf
插入大量数据</
Redis主动内存碎片整理详解

本文详细介绍了Redis的主动内存碎片整理功能,该功能允许在服务器运行时对内存中的数据进行整理,以减少内存碎片。主动碎片整理在碎片比例超过一定阈值时启动,利用Jemalloc特性创建连续内存区域并释放旧数据。在测试过程中,随着大量数据的删除,主动碎片整理开始运行,内存使用情况和碎片率显著改善。配置参数可以调整整理效果,该功能默认关闭,仅在编译时使用特定版本的Jemalloc时可用。
最低0.47元/天 解锁文章
1899

被折叠的 条评论
为什么被折叠?



