查看 Redis 某个数据库的内存占用
1. 创建一个 size.lua 脚本文件
local total = 0
local cursor = "0"
repeatlocal reply = redis.call("SCAN", cursor, "COUNT", 1000)cursor = reply[1]for _, key in ipairs(reply[2]) dototal = total + redis.call("MEMORY", "USAGE", key)end
until cursor == "0"
return string.format("Total memory: %.2f MB", total / (1024 * 1024))
2. 命令行调用
# 例如,计算 db15 的内存使用情况
$redis-cli -n 15 --eval size.lua
"Total memory: 2.99 MB"