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会进行一定的预留,进而引 起了内存碎片。
以redis为例说明内存碎片问题
最新推荐文章于 2024-10-08 14:37:30 发布