在Debian上优化Redis内存使用可从配置、数据结构、系统层面入手,具体如下:
redis.conf
,设置maxmemory
参数(如maxmemory 4gb
),留出20%-30%内存给系统。maxmemory-policy
,缓存场景用allkeys-lru
,持久化场景用volatile-lru
。ziplist
压缩小数据(如小List/Set),通过redis.conf
设置list-max-ziplist-entries
等参数。EXPIRE
,自动清理无用数据。activedefrag yes
开启自动碎片整理。/etc/security/limits.conf
,设置redis soft nofile 65535
。taskset
命令将Redis进程绑定到特定CPU核心,减少上下文切换。INFO memory
监控内存使用,重点关注used_memory
和mem_fragmentation_ratio
。SLOWLOG
命令找出高耗时命令并优化。参考来源: