当前位置:   article > 正文

以redis为例说明内存碎片问题_redis为会出现内存碎片

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会进行一定的预留,进而引
起了内存碎片。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/755808?site
推荐阅读
相关标签
  

闽ICP备14008679号