Redis用于保存数据,但占用大量内存,内存占用高达52.5%。 我在redis中删除了一半的key,删除操作的返回码是可以的,但是内存占用并没有减少。
这是什么原因?提前致谢。
我的操作代码如下:
// 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());
redis 信息:
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
最佳答案
请引用以下链接的内存分配部分:
http://redis.io/topics/memory-optimization
我在这里引用了它:
Redis will not always free up (return) memory to the OS when keys are removed. This is not something special about Redis, but it is how most malloc() implementations work. For example if you fill an instance with 5GB worth of data, and then remove the equivalent of 2GB of data, the Resident Set Size (also known as the RSS, which is the number of memory pages consumed by the process) will probably still be around 5GB, even if Redis will claim that the user memory is around 3GB. This happens because the underlying allocator can't easily release the memory. For example often most of the removed keys were allocated in the same pages as the other keys that still exist.
关于memory-management - 为什么删除一半键时redis内存使用量没有减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11698911/