- 使用redis-cli连接到redis服务器,然后执行info命令,查看返回的信息中的memory部分,其中包含了redis占用的内存、内存限制、内存碎片等信息。
$ redis-cli
127.0.0.1:6379> info
# Memory
used_memory:1234567
used_memory_human:1.18M
used_memory_rss:2345678
used_memory_rss_human:2.24M
used_memory_peak:3456789
used_memory_peak_human:3.30M
used_memory_peak_perc:35.64%
used_memory_overhead:456789
used_memory_startup:789012
used_memory_dataset:777778
used_memory_dataset_perc:63.01%
allocator_allocated:8901234
allocator_active:9012345
allocator_resident:9123456
total_system_memory:1023456789
total_system_memory_human:973.83M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0 # no limit set!
maxmemory_human:0B # no limit set!
maxmemory_policy:noeviction # no eviction policy set!
allocator_frag_ratio:1.01 # low fragmentation ratio!
allocator_frag_bytes:111111 # low fragmentation bytes!
allocator_rss_ratio=1.01 # low rss ratio!
allocator_rss_bytes:-11111 # negative rss bytes!
rss_overhead_ratio:-1.00 # negative overhead ratio!
rss_overhead_bytes:-1234567 # negative overhead bytes!
mem_fragmentation_ratio:-2.00 # negative fragmentation ratio!
mem_fragmentation_bytes:-2345678 # negative fragmentation bytes!
mem_not_counted_for_evict:-3456789 # negative memory not counted for eviction!
mem_replication_backlog:-4567890 # negative replication backlog memory!
mem_clients_slaves:-5678901 # negative clients slaves memory!
mem_clients_normal:-6789012 # negative clients normal memory!
mem_aof_buffer:-7890123 # negative aof buffer memory!
- 使用redis-cli连接到redis服务器,然后执行config get maxmemory命令,查看返回的信息中的maxmemory值,这是redis设置的最大内存限制。如果这个值为0,表示没有限制。您可以使用config set maxmemory <bytes>命令来修改这个值。
$ redis-cli
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0" // no limit set!
127.0.0.1:6379> config set maxmemory 100000000 // set the limit to 100MB
OK
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "100000000" // the limit is now 100MB
- 使用redis-cli连接到redis服务器,然后执行dbsize命令,查看返回的信息中的dbsize值,这是当前数据库中键的数量。您可以使用keys *命令来查看所有键的详细信息。
$ redis-cli
127.0.0.1:6379> dbsize
(integer) 10 // there are 10 keys in the database
127 .0 . 0 . 1 : 6379 > keys * // list all keys and their types
1 ) "key1" (string)
2 ) "key2" (hash)
3 ) "key3" (list)
4 ) "key4" (set)
5 ) "key5" (zset)
6 ) "key6" (bitmap)
7 ) "key7" (hyperloglog)
8 ) "key8" (stream)
9 ) "key9" (module)
10 ) "key10"(geo)
- 使用top或者ps等系统命令,查看redis-server进程占用的物理内存和虚拟内存情况。
$ top -p $(pidof redis-server)
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 redis 20 0 200m 50m 5m S 5 5 - redis-server *
如果您发现redis占用了过多的空间,您可以采取以下措施来释放空间:
- 删除不需要或者过期的键值对,使用del或者expire等命令。
$ redis-cli
127.0.0.1:6379> del key1 // delete the key1
(integer) 1 // the number of keys deleted
127.0.0.1:6379> expire key2 10 // set the key2 to expire in 10 seconds
(integer) 1 // the number of keys affected
- 优化数据结构和编码方式,使用更紧凑和高效的数据类型和编码格式。
$ redis-cli
127.0.0.1:6379> object encoding key3 // check the encoding of key3
"quicklist" // a list encoded as a linked list of ziplists
127.0.0.1:6379> lpush key3 "a very long string that will cause memory waste" // add a long string to the list
(integer) 4 // the length of the list
127 .0 . 0 . 1 : 6379 > object encoding key3 // check the encoding again
"linkedlist" // now the list is encoded as a linked list of strings, which is less efficient
127 .0 . 0 . 1 : 6379 > memory usage key3 // check the memory usage of key3
(integer) 1234 // some bytes used by key3
127 .0 . 0 . 1 : 6379 > lpop key3 // remove the long string from the list
"a very long string that will cause memory waste"
127 .0 . 0 . 1 : 6379 > object encoding key3 // check the encoding again
"quicklist" // now the list is encoded back to a quicklist, which is more efficient
127 .0 . 0 . 1 : 6379 > memory usage key3 // check the memory usage again
(integer) -111 # some bytes saved by key3
- 启用数据持久化功能,将数据保存到磁盘上,并定期清理旧的备份文件。
$ redis-cli
127.0.0.1:6379> config get save # check the save configuration
1 ) "save"
2 ) "900 # save after every seconds if at least keys changed \n300 # save after every seconds if at least keys changed \n60 # save after every seconds if at least keys changed "
# this means that redis will create a snapshot file (dump.rdb) in certain conditions
# you can modify this configuration using config set save <parameters> command
# you can also manually trigger a snapshot using save or bgsave command
# you can also enable append-only file (appendonly.aof) for more durability using config set appendonly yes command
# you should periodically delete old backup files to free up disk space
- 启用数据压缩功能,使用LZF算法对字符串值进行压缩。
$ redis-cli
127.0.0.1:6379> config get compression # check if compression is enabled
(error) ERR unknown parameter 'compression' # compression is not enabled by default
# you can enable compression using config set compression yes command
# this will apply LZF algorithm to compress string values larger than certain threshold
# you can also adjust the compression threshold using config set compression-threshold <bytes> command
# you can also disable compression for certain keys using config set compression-excluded-keys <pattern> command
- 启用内存淘汰策略,在达到最大内存限制时自动删除一些键值对。您可以使用config get maxmemory-policy命令来查看当前策略,并使用config set maxmemory-policy <policy>命令来修改策略。常见的策略有volatile-lru、allkeys-lru、volatile-random、allkeys-random等。
$ redis-cli
127.0.0.1:6379> config get maxmemory-policy # check current eviction policy
(error) ERR unknown