2023.03.21 如何查看和释放Redis占用的内存空间

2023-03-20  本文已影响0人  薛定谔的猴子
$ 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 
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 
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 -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占用了过多的空间,您可以采取以下措施来释放空间:

$ 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  
$ 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  
$ redis-cli 
127.0.0.1:6379> config get maxmemory-policy # check current eviction policy 
(error) ERR unknown
上一篇 下一篇

猜你喜欢

热点阅读