What is memory fragmentation?

I’ve heard the term “memory fragmentation” used a few times in the context of C++ dynamic memory allocation. I’ve found some questions about how to deal with memory fragmentation, but can’t find a direct question that deals with it itself. So:

  • What is memory fragmentation?
  • How can I tell if memory fragmentation is a problem for my application? What kind of program is most likely to suffer?
  • What are good common ways to deal with memory fragmentation?

Also:

  • I’ve heard using dynamic allocations a lot can increase memory fragmentation. Is this true? In the context of C++, I understand all the standard containers (std::string, std::vector, etc) use dynamic memory allocation. If these are used throughout a program (especially std::string), is memory fragmentation more likely to be a problem?
  • How can memory fragmentation be dealt with in an STL-heavy application?

12 Answers
12

Leave a Comment