redis内存泄露问题
2015-07-22 16:01
591 查看
Memory allocation
To store user keys, Redis allocates at most as much memory as themaxmemorysetting enables (however there are small extra allocations possible).
The exact value can be set in the configuration file or set later via CONFIG SET (see Using
memory as an LRU cache for more info). There are a few things that should be noted about how Redis manages memory:
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.
The previous point means that you need to provision memory based on your peak memory usage. If your workload from time to time requires 10GB, even if most of the times 5GB could do, you need to provision
for 10GB.
However allocators are smart and are able to reuse free chunks of memory, so after you freed 2GB of your 5GB data set, when you start adding more keys again, you'll see the RSS (Resident Set Size) to stay steady and don't grow more, as you add up to 2GB
of additional keys. The allocator is basically trying to reuse the 2GB of memory previously (logically) freed.
Because of all this, the fragmentation ratio is not reliable when you had a memory usage that at peak is much larger than the currently used memory. The fragmentation is calculated as the amount of memory currently in use (as the sum of all the allocations
performed by Redis) divided by the physical memory actually used (the RSS value). Because the RSS reflects the peak memory, when the (virtually) used memory is low since a lot of keys / values were freed, but the RSS is high, the ratio
mem_used / RSSwill be very high.
If
maxmemoryis not set Redis will keep allocating memory as it finds fit and thus it can (gradually) eat up all your free memory. Therefore it is generally
advisable to configure some limit. You may also want to set
maxmemory-policyto
noeviction(which
is not the default value in some older versions of Redis).
It makes Redis return an out of memory error for write commands if and when it reaches the limit - which in turn may result in errors in the application but will not render the whole machine dead because of memory starvation.
相关文章推荐
- redis 学习指南
- 利用Redis BitMap 统计用户活跃指标
- Redis 3.0官方文档翻译计划(2) ——从入门到精通(上)
- Redis 3.0官方文档翻译计划(3) ——从入门到精通(中)
- Redis 3.0官方文档翻译计划(4) ——从入门到精通(下)
- Redis 3.0官方文档翻译计划(1) ——Redis介绍
- redis + Jedis
- 基于zookeeper的redis高可用
- redis删除数据后内存释放问题
- Windows下安装并设置Redis
- shell 例程 —— 解决redis读取稳定性
- 为Symfony2和Redis正名,基于PHP的10亿请求/周网站打造
- redis加mybatis,org.apache.commons.pool2.impl.GenericObjectPoolConfig记住这个是pool2!!
- Redis 集群解决方案 Codis
- 基于Redis实现分布式锁
- redis数据丢失及解决
- Redis命令小细节
- Redis学习笔记---安装
- redis 五种数据类型的使用场景
- Redis缓存