Redis-Memory-Usage
对于redis来说,什么是最重要的?
毋庸置疑,是内存。
一、reids 内存分析
redis内存使用情况:info memory
| 属性名 | 属性说明 |
|---|---|
| used memory | Redis 分配器分配的内存总量,也就是内部存储的所有数据内存占用量 |
| used memory human | 以可读的格式返回used memory |
| used memory_rss | 从操作系统的角度显示 Redis 进程占用的物理内存总量 |
| used memory_peak | 内存使用的最大值,表示used memory 的峰值 |
| used memory peak human | 以可读的格式返回used memory peak |
| used memory_lua | Lua 引擎所消耗的内存大小 |
| mem fraqmentation ratio | used memory_rss/used memory 比值,表示内存碎片率 |
| mem allocator | Redis 所使用的内存分配器。默认为jemalloc |
示例:
127.0.0.1:6379> info memory
# Memory
used_memory:873848
used_memory_human:853.37K
used_memory_rss:14249984
used_memory_rss_human:13.59M
used_memory_peak:933720
used_memory_peak_human:911.84K
used_memory_peak_perc:93.59%
used_memory_overhead:832360
used_memory_startup:811864
used_memory_dataset:41488
used_memory_dataset_perc:66.93%
allocator_allocated:1032352
allocator_active:1253376
allocator_resident:4968448
total_system_memory:13137526784
total_system_memory_human:12.24G
used_memory_lua:30720
used_memory_lua_human:30.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.21
allocator_frag_bytes:221024
allocator_rss_ratio:3.96
allocator_rss_bytes:3715072
rss_overhead_ratio:2.87
rss_overhead_bytes:9281536
mem_fragmentation_ratio:17.11
mem_fragmentation_bytes:13417160
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:20496
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0
可以看到,当前节点内存碎片率为226893824/209522728≈1.08,使用的内存分配器是jemalloc。
used_memory_rss 通常情况下是大于 used_memory 的,因为内存碎片的存在。
但是当操作系统把redis内存swap到硬盘时,memory_fragmentation_ratio 会小于1。redis使用硬盘作为内存,因为硬盘的速度,redis性能会受到极大的影响。
二、redis 内存使用
之前的文章 关于redis,你需要了解的几点!中我们简单介绍过redis的内存使用分布:自身内存,键值对象占用、缓冲区内存占用及内存碎片占用。
redis 空进程自身消耗非常的少,可以忽略不计,优化内存可以不考虑此处的因素。
1、对象内存
对象内存,也即真实存储的数据所占用的内存。
redis k-v结构存储,对象占用可以简单的理解为 k-size + v-size。
redis的键统一都为字符串类型,值包含多种类型:string、list、hash、set、zset五种基本类型及基于string的Bitmaps和HyperLogLog类型等。
在实际的应用中,一定要做好kv的构建形式及内存使用预期,可以参考 关于redis,你需要了解的几点!中关于不同值类型不同形式下的内部存储实现介绍。
2、缓冲内存
缓冲内存包括三部分:客户端缓存、复制积压缓存及AOF缓冲区。