跳到主要内容
安全

解决Redis内存占用过高问题

通过 2025年9月6日 没有评论

问题:32G物理内存VM,Redis一起来进程就占用了13.36G,从趋势看redis内存占用越来越高,导致频繁触发告警

500:C 06 Sep 2025 09:04:08.948 * Redis version=8.0.3, bits=64, commit=00000000, modified=0, pid=500, just started
500:C 06 Sep 2025 09:04:08.948 * Configuration loaded
500:M 06 Sep 2025 09:04:08.948 * monotonic clock: POSIX clock_gettime
500:M 06 Sep 2025 09:04:08.949 * Running mode=standalone, port=6379.
500:M 06 Sep 2025 09:04:08.949 * Server initialized
500:M 06 Sep 2025 09:04:08.950 * Loading RDB produced by version 8.0.3
500:M 06 Sep 2025 09:04:08.950 * RDB age 235 seconds
500:M 06 Sep 2025 09:04:08.950 * RDB memory usage when created 13684.27 Mb
500:M 06 Sep 2025 09:04:27.417 * Done loading RDB, keys loaded: 4090, keys expired: 11.
500:M 06 Sep 2025 09:04:27.417 * DB loaded from disk: 18.467 seconds
500:M 06 Sep 2025 09:04:27.417 * Ready to accept connections tcp

排查:先连接redis-cli,看下当前的情况:

$ redis-cli -h 10.1.2.3 -p 6379
10.1.2.3:6379> info MEMORY
NOAUTH Authentication required.

#要把Redis密码写到环境变量
10.1.2.3:6379> exit
$ vi /opt/redis-8.0.3/redis.conf
$ export REDISCLI_AUTH=Pass50rd
$ redis-cli -h 10.1.2.3 -p 6379
10.1.2.3:6379> info MEMORY

Memory

used_memory:14348737944
used_memory_human:13.36G
used_memory_rss:13159550976
used_memory_rss_human:12.26G
used_memory_peak:14361437440
used_memory_peak_human:13.38G
used_memory_peak_perc:99.91%
used_memory_overhead:930160
used_memory_startup:623000
used_memory_dataset:14347807784
used_memory_dataset_perc:100.00%
allocator_allocated:14349593504
allocator_active:14350082048
allocator_resident:14384250880
allocator_muzzy:0
total_system_memory:32930652160
total_system_memory_human:30.67G
used_memory_lua:31744
used_memory_vm_eval:31744
used_memory_lua_human:31.00K
used_memory_scripts_eval:0
number_of_cached_scripts:0
number_of_functions:0
number_of_libraries:0
used_memory_vm_functions:32768
used_memory_vm_total:64512
used_memory_vm_total_human:63.00K
used_memory_functions:192
used_memory_scripts:192
used_memory_scripts_human:192B
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.00
allocator_frag_bytes:412512
allocator_rss_ratio:1.00
allocator_rss_bytes:34168832
rss_overhead_ratio:0.91
rss_overhead_bytes:-1224699904
mem_fragmentation_ratio:0.92
mem_fragmentation_bytes:-1189186768
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_total_replication_buffers:0
mem_replica_full_sync_buffer:0
mem_clients_slaves:0
mem_clients_normal:17280
mem_cluster_links:0
mem_aof_buffer:0
mem_allocator:jemalloc-5.3.0
mem_overhead_db_hashtable_rehashing:0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0

于是,编辑/opt/redis-8.0.3/redis.conf,加入两行:

# allkeys-lru:在所有键中,选取最近最少使用的数据删除

重启redis服务后,可以看到占用内存固定在8GB。

10.1.2.3:6379> INFO MEMORY

Memory

used_memory:8585111184
used_memory_human:8.00G
used_memory_rss:7882973184
used_memory_rss_human:7.34G
used_memory_peak:14348443976
used_memory_peak_human:13.36G
used_memory_peak_perc:59.83%
used_memory_overhead:855560
used_memory_startup:623000
used_memory_dataset:8584255624
used_memory_dataset_perc:100.00%
allocator_allocated:8585999480
allocator_active:8591949824
allocator_resident:8628191232
allocator_muzzy:0
total_system_memory:32930652160
total_system_memory_human:30.67G
used_memory_lua:31744
used_memory_vm_eval:31744
used_memory_lua_human:31.00K
used_memory_scripts_eval:0
number_of_cached_scripts:0
number_of_functions:0
number_of_libraries:0
used_memory_vm_functions:32768
used_memory_vm_total:64512
used_memory_vm_total_human:63.00K
used_memory_functions:192
used_memory_scripts:192
used_memory_scripts_human:192B
maxmemory:8589934592
maxmemory_human:8.00G
maxmemory_policy:allkeys-lru
allocator_frag_ratio:1.00
allocator_frag_bytes:5874312
allocator_rss_ratio:1.00
allocator_rss_bytes:36241408
rss_overhead_ratio:0.91
rss_overhead_bytes:-745218048
mem_fragmentation_ratio:0.92
mem_fragmentation_bytes:-702137456
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_total_replication_buffers:0
mem_replica_full_sync_buffer:0
mem_clients_slaves:0
mem_clients_normal:17280
mem_cluster_links:0
mem_aof_buffer:0
mem_allocator:jemalloc-5.3.0
mem_overhead_db_hashtable_rehashing:0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0
10.1.2.3:6379> INFO STATS

Stats

total_connections_received:24
total_commands_processed:549
instantaneous_ops_per_sec:0
total_net_input_bytes:11554504
total_net_output_bytes:9592186
total_net_repl_input_bytes:0
total_net_repl_output_bytes:0
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
instantaneous_input_repl_kbps:0.00
instantaneous_output_repl_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_subkeys:0
expired_keys:5
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:42
evicted_keys:1748
evicted_clients:0
evicted_scripts:0
total_eviction_exceeded_time:300
current_eviction_exceeded_time:0
keyspace_hits:142
keyspace_misses:6
pubsub_channels:0
pubsub_patterns:0
pubsubshard_channels:0
latest_fork_usec:117887
total_forks:1
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
total_active_defrag_time:0
current_active_defrag_time:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:4
dump_payload_sanitizations:0
total_reads_processed:623
total_writes_processed:626
io_threaded_reads_processed:0
io_threaded_writes_processed:0
client_query_buffer_limit_disconnections:0
client_output_buffer_limit_disconnections:0
reply_buffer_shrinks:40
reply_buffer_expands:29
eventloop_cycles:3633
eventloop_duration_sum:1396841
eventloop_duration_cmd_sum:17155
instantaneous_eventloop_cycles_per_sec:9
instantaneous_eventloop_duration_usec:449
acl_access_denied_auth:0
acl_access_denied_cmd:0
acl_access_denied_key:0
acl_access_denied_channel:0
10.1.2.3:6379> RANDOMKEY
“2019006611”
10.1.2.3:6379> TTL 2019006611
(integer) -1
10.1.2.3:6379> MEMORY USAGE 2019006611
(integer) 3465027
10.1.2.3:6379> INFO keyspace

Keyspace

db0:keys=1811,expires=136,avg_ttl=37416083,subexpiry=0
db1:keys=534,expires=10,avg_ttl=1529721,subexpiry=0
db8:keys=18,expires=18,avg_ttl=303328,subexpiry=0

参考文档:运维排查篇 | Redis占用内存过高怎么办

留下回复