为什么在删除一半的key时redis内存使用量没有减少

为什么在删除一半的key时redis内存使用量没有减少

本文介绍了为什么在删除一半的key时redis内存使用量没有减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Redis 是用来保存数据的,但是内存消耗很大,内存使用率高达 52.5%.我把redis里面的key删了一半,删除操作的返回码没问题,但是内存占用并没有减少.

是什么原因?提前致谢.

我的操作代码如下:

//保存数据m_pReply = (redisReply *)redisCommand(m_pCntxt, "set %b %b", mykey.data(), mykey.size(), &myval, sizeof(myval));//删除数据m_pReply = (redisReply *)redisCommand(m_pCntxt, "del %b", mykey.data(), mykey.size());

redis 信息:

redis 127.0.0.1:6979> 信息redis_version:2.4.8redis_git_sha1:00000000redis_git_dirty:0arch_bits:64多路复用API:epollgcc_version:4.4.6process_id:28799正常运行时间_in_seconds:1289592正常运行时间_in_days:14lru_clock:127925used_cpu_sys:148455.30used_cpu_user:38023.92used_cpu_sys_children:23187.60used_cpu_user_children:123989.72connected_clients:22connected_slaves:0client_longest_output_list:0client_biggest_input_buf:0被阻止的客户端:0used_memory:31903334872used_memory_human:29.71Gused_memory_rss:34414981120used_memory_peak:34015653264used_memory_peak_human:31.68Gmem_fragmentation_ratio:1.08mem_allocator:jemalloc-2.2.5加载:0aof_enabled:0changes_since_last_save:177467bgsave_in_progress:0last_save_time:1343456339bgrewriteaof_in_progress:0total_connections_received:820total_commands_processed:2412759064expired_keys:0evicted_keys:0keyspace_hits:994257907keyspace_misses:32760132pubsub_channels:0pubsub_patterns:0latest_fork_usec:11672476vm_enabled:0角色:奴隶master_host:192.168.252.103master_port:6479master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0db0:keys=66372158,expires=0
解决方案

请参考以下链接的内存分配部分:

http://redis.io/topics/memory-optimization

我在这里引用了:

当键被删除时,Redis 不会总是向操作系统释放(返回)内存移除.这并不是 Redis 的特别之处,但它是最重要的malloc() 实现工作.例如,如果您填充一个实例5GB的数据,然后删除相当于2GB的数据,常驻集大小(也称为 RSS,它是进程消耗的内存页)可能仍然存在5GB,即使Redis会声称用户内存在3GB左右.这发生是因为底层分配器不能轻易释放记忆.例如,通常大部分删除的键都分配在与仍然存在的其他键相同的页面.

Redis is used to save data but it costs a lot of memory, and its memory usage up to 52.5%.I deleted half of the keys in redis, and the return code of the delete operation is ok, but its memory usage doesn't reduce.

What's the reason? Thanks in Advance.

My operation code is as below:

// save data
m_pReply = (redisReply *)redisCommand(m_pCntxt, "set %b %b", mykey.data(), mykey.size(), &myval, sizeof(myval));
// del data
m_pReply = (redisReply *)redisCommand(m_pCntxt, "del %b", mykey.data(), mykey.size());

The redis info:

redis 127.0.0.1:6979> info
redis_version:2.4.8
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.6
process_id:28799
uptime_in_seconds:1289592
uptime_in_days:14
lru_clock:127925
used_cpu_sys:148455.30
used_cpu_user:38023.92
used_cpu_sys_children:23187.60
used_cpu_user_children:123989.72
connected_clients:22
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
used_memory:31903334872
used_memory_human:29.71G
used_memory_rss:34414981120
used_memory_peak:34015653264
used_memory_peak_human:31.68G
mem_fragmentation_ratio:1.08
mem_allocator:jemalloc-2.2.5
loading:0
aof_enabled:0
changes_since_last_save:177467
bgsave_in_progress:0
last_save_time:1343456339
bgrewriteaof_in_progress:0
total_connections_received:820
total_commands_processed:2412759064
expired_keys:0
evicted_keys:0
keyspace_hits:994257907
keyspace_misses:32760132
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:11672476
vm_enabled:0
role:slave
master_host:192.168.252.103
master_port:6479
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
db0:keys=66372158,expires=0
解决方案

Please refer to Memory allocation section on the following link:

http://redis.io/topics/memory-optimization

I quoted it here:

这篇关于为什么在删除一半的key时redis内存使用量没有减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:13