Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

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

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.

Let's assume that I've done my best to minimize runtime heap fragmentation in my VC++ server application but still it builds up and causes problems. I could for example automatically restart the application every month or every half million requests processed - safely stop it and safely start again with a new heap. What else can I do to workaround heap fragmentation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

same answer as here: How to detect and estimate heap fragmentation in my C++ program?

write your own memory manager adapted to your memory allocation patterns. Or buy one (for example smart-heap).

Since the fragmentation depends on your memory allocation patterns / freeing patterns, a better answer is difficult to give. But you could take a look into fixed size allocators or take a look at the smart heap page how they handle allocation. There are also a lot of papers on this topic. Try for example www.memorymanagement.org

Or you could take a look at FastMM4 - which is open source, but in Pascal/Delphi

There are some programming techniques as well. Most notably: the Object Pool. In this case, there is no fragmentation, as the objects are re-used and not freed. But I think a fixed size allocator performs better than the Object Pool. The Object Pool used this way is only a "poor mans" fixed size allocator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...