以redis为例说明内存碎片问题

When Redis runs, lots of different things are allocated, such as dictionaries to allow O(1) lookups, metadata for 
objects, VM metadata. All these things need to be in memory, but aren't there from the start. So, when you 
gradually add a lot of data to an empty db, these things are allocated as well. Now, when these objects are 
free'd, some other objects are not (like some dictionary stuff, etc). This causes heavy memory fragmentation. 
When the operating system hands out blocks of memory, these blocks are often 4096 bytes in size. When 
there is only 1 byte allocated by Redis on this 4k block, the OS sees this entire block as being used by Redis.

To return to your question: when there is metadata that is not free'd on FLUSHALL and it quit heavily 
fragmented, there are a lot of gaps of unused memory in the entire space occupied by Redis. When new 
objects are again allocated, these gaps are used. You will see that if you load the dataset again, you will not 
see (a large) increase in memory usage. I hope this helps in understanding these memory properties!

上面的分析使我对之前遇到的一个问题豁然开朗,当时是自己写的一个TCP服务端程序,它不停的接收数据包并处理。但当它处于空闲状态时,是没有大量内存使用的,但是ps的
结果上看使用内存使用量(VIRT)往往达到百兆以上,其原因就是因为使用了STL的QUEUE来存储收到的数据包,当所有的数据包空间被释放后,QUEUE会进行一定的预留,进而引
起了内存碎片。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值