Redis内存占用分析与优化
实例级内存使用情况检查
通过 INFO memory 命令获取当前Redis实例的内存统计信息:
redis> INFO memory
# Memory
used_memory:227329144
used_memory_human:216.80M
used_memory_rss:361926656
used_memory_peak:2176294808
maxmemory:2147483648
mem_fragmentation_ratio:1.59
重点关注以下指标:
used_memory:实际使用的内存(约216.8MB)used_memory_rss:操作系统分配的物理内存(约345.16MB)mem_fragmentation_ratio:内存碎片率,超过1.5可能需要关注maxmemory:最大内存限制(2GB)
按数据库分片分析内存分布
使用Lua脚本对各DB的内存使用进行细分统计:
redis> EVAL "
redis.replicate_commands()
local total = tonumber(ARGV[1])
local dbs = {}
for i = 2, #ARGV do
dbs[#dbs+1] = tonumber(ARGV[i])
end
local result = {}
for _, db_idx in ipairs(dbs) do
redis.call('SELECT', db_idx)
local cursor, sum_bytes = '0', 0
repeat
local batch = redis.call('SCAN', cursor, 'COUNT', 1000)
cursor = batch[1]
for _, key in ipairs(batch[2]) do
sum_bytes = sum_bytes + (redis.call('MEMORY', 'USAGE', key) or 0)
end
until cursor == '0'
local mb = sum_bytes / 1024 / 1024
local pct = sum_bytes * 100 / total
table.insert(result, string.format('DB%d: %.2f MB (%.2f%%)', db_idx, mb, pct))
end
redis.call('SELECT', 0)
return result
" 0 227329144 0 5 6 7
输出结果表明:DB7 占比达13.64%,是主要内存消耗源。
定位高内存占用Key
切换至目标数据库后,扫描并排序大键:
redis> SELECT 7
redis> EVAL "
local cursor = '0'
local entries = {}
repeat
local scan_result = redis.call('SCAN', cursor)
cursor = scan_result[1]
for _, key in ipairs(scan_result[2]) do
local size = redis.call('MEMORY', 'USAGE', key) or 0
local ttl = redis.call('TTL', key) or -1
table.insert(entries, {key, size, ttl})
end
until cursor == '0'
table.sort(entries, function(a,b) return a[2] > b[2] end)
local top100 = {}
for i = 1, math.min(100, #entries) do
table.insert(top100, entries[i])
end
return top100
" 0
前缀匹配内存分析
针对特定前缀如 CASES_WORK 的键进行聚合分析:
redis> EVAL "
local prefix = 'CASES_WORK'
local cursor, total_size = '0', 0
repeat
local res = redis.call('SCAN', cursor, 'MATCH', prefix .. '*', 'COUNT', 1000)
cursor = res[1]
for _, key in ipairs(res[2]) do
total_size = total_size + (redis.call('MEMORY', 'USAGE', key) or 0)
end
until cursor == '0'
local info = redis.call('INFO', 'memory')
local total_mem = tonumber(string.match(info, 'used_memory:(%d+)'))
local percentage = total_size * 100 / total_mem
return { total_size, total_mem, string.format('%.2f%%', percentage) }
" 0
返回结果为:{2616464, 220134304, "1.19%"},确认该前缀共占约1.19%内存。
批量清理操作
安全地删除指定前缀的键,支持分批执行:
redis> SELECT 7
-- 分批删除,每次最多处理1000个
redis> EVAL "
redis.replicate_commands()
local cursor = '0'
local deleted_count = 0
local limit = tonumber(ARGV[1])
repeat
local res = redis.call('SCAN', cursor, 'MATCH', 'CASES_WORK*', 'COUNT', 1000)
cursor = res[1]
for _, key in ipairs(res[2]) do
if deleted_count < limit then
redis.call('UNLINK', key)
deleted_count = deleted_count + 1
end
end
until cursor == '0' or deleted_count >= limit
return deleted_count
" 0 1000
统计指定前缀键数量
redis> EVAL "
local cursor = '0'
local count = 0
repeat
local res = redis.call('SCAN', cursor, 'MATCH', 'CASES_WORK*', 'COUNT', 1000)
cursor = res[1]
count = count + #res[2]
until cursor == '0'
return count
" 0
最终返回值为 299,说明共有299个以 CASES_WORK 开头的键。