在 CentOS 上调整 Redis 内存,通常是从 Redis 配置层面 和 系统层面 两方面来做。下面按常见场景说明。
编辑 Redis 配置文件(常见路径):
/etc/redis.conf
# 或
/etc/redis/redis.conf
找到或添加:
maxmemory 2gb
可选单位:
1gb512mb1024mb当内存达到上限时如何处理:
maxmemory-policy allkeys-lru
常见策略:
noeviction:不淘汰(写报错)allkeys-lru:所有 key LRU 淘汰volatile-lru:只淘汰带过期时间的 keyallkeys-random:随机淘汰修改后重启 Redis:
systemctl restart redis
redis-cli
info memory
重点关注:
used_memoryused_memory_humanmaxmemory编辑:
/etc/sysctl.conf
添加:
vm.overcommit_memory = 1
生效:
sysctl -p
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p
docker run -d \
--name redis \
-m 2g \
redis:7 \
redis-server --maxmemory 2gb --maxmemory-policy allkeys-lru
mem_fragmentation_ratioactivedefrag yessystemctl 启动的如果你能告诉我:
我可以给你更精确的配置示例。