1. 问题
日志: OOM command not allowed when used memory > ‘maxmemory’
2. 定位
1. 查看内存使用情况
info memory
详情参考: [Redis info 命令](https://redis.io/commands/info)
# Memory
used_memory:855305648
used_memory_human:815.68M // allocator已分配的内存
used_memory_rss:994541568
used_memory_rss_human:948.47M // 操作系统已分配的内存, 和top及ps相同
used_memory_peak:12287832312
used_memory_peak_human:11.44G // 内存消耗峰值
used_memory_peak_perc:6.96%
used_memory_overhead:13178116
used_memory_startup:1050144
used_memory_dataset:842127532
used_memory_dataset_perc:98.58%
allocator_allocated:856281040
allocator_active:856739840
allocator_resident:997576704
total_system_memory:270291668992
total_system_memory_human:251.73G // 机器总内存
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:8589934592
maxmemory_human:8.00G // redis.conf 配置的 "最大内存"
maxmemory_policy:volatile-lru // redis.conf 配置的 "最大内存策略"
allocator_frag_ratio:1.00
allocator_frag_bytes:458800
allocator_rss_ratio:1.16
allocator_rss_bytes:140836864
rss_overhead_ratio:1.00
rss_overhead_bytes:-3035136
mem_fragmentation_ratio:1.16 // 比率, used_memory_rss / used_memory
mem_fragmentation_bytes:139277848
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:302820
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0 // allocator (Redis内存分配器)
active_defrag_running:0
lazyfree_pending_objects:0
2. 查看有多少DB
config get databases
3. 切换DB
select 0
4. 查看当前DB, 键的数量
dbsize
keys命令慎用, 会直接卡死
5. Redis最大内存
config get maxmemory
参考内存资源情况, 可在redis.conf中调整
6. Redis最大内存策略
config get maxmemory-policy
参考内存资源情况, 可在redis.conf中调整
maxmemory-policy 六种方式
1、volatile-lru: 只对设置了过期时间的key进行LRU(默认值)
2、allkeys-lru: 删除lru算法的key
3、volatile-random: 随机删除即将过期key
4、allkeys-random: 随机删除
5、volatile-ttl: 删除即将过期的
6、noeviction: 永不过期,返回错误
7. 清空数据库
慎用! 仅仅用于紧急情况, 且应用不强依赖于Redis, 且流量不甚大.
flushdb: 清空当前数据库中的所有 key
flushall: 删除所有数据库的所有 key
3. 原因
-
Redis问题
-
Redis最大内存 过小
-
Redis最大内存策略 不当
默认的volatile-lru已经够了, 不宜再做调整
-
-
应用问题
- 应用缺陷, 导致存储了大量无过期时间的键 (大都此问题!!!)
4. 方案
-
应用层面做好规范
-
运维层面做好监控