How do I workaround heap fragmentation in a C++ server program?

后端 未结 4 1977
小蘑菇
小蘑菇 2020-12-19 19:57

Heap fragmentation can cause a server application that is expected to run continuously for many months to suddenly start malfunctioning thinking that it\'s out of memory.

4条回答
  •  星月不相逢
    2020-12-19 20:29

    Rather than scheduling your restart based on time or number of requests, you could examine the heap to see when the fragmentation has reached a level when the largest contiguous block of memory falls below a certain level - after all - you'll start to see out of memory errors when not when all memory is used up but when you try and allocate objects larger than the size of the biggest free contiguous space in the heap.

    You can use VirtualQueryEx to walk your heap and find the largest free contiguous area. For an example of how to do this, see this article.

提交回复
热议问题