I'm working on a large server application written using C++. This server needs to run possibly for months without restarting. Fragmentation is already a suspected issue here, since our memory consumption increases over time. So far the measurement has been to compare private bytes with virtual bytes, and analyze the difference in those two numbers.
My general approach to fragmentation is to leave it to analysis. I have the same way of thinking about other things like general performance and memory optimizations. You have to back up the changes with analysis and proof.
I'm noticing a lot during code reviews or discussions, that memory fragmentation is one of the first things that comes up. It's almost like there's a huge fear of it now, and there's a big initiative to "prevent fragmentation" ahead of time. Code changes are requested that seem favorable to reducing or preventing memory fragmentation problems. I tend to disagree with these right off the bat since they seem like premature optimization to me. I would be sacrificing code cleanliness/readability/maintainability/etc. in order to satisfy these changes.
For example, take the following code:
std::stringstream s;
s << "This" << "Is" << "a" << "string";
Above, the number of allocations the stringstream makes here is undefined, it could be 4 allocations, or just 1 allocation. So we can't optimize based on that alone, but the general consensus is to either use a fixed buffer or somehow modify the code to potentially use less allocations. I don't really see the stringstream expanding itself here as a huge contributor to memory problems, but maybe I'm wrong.
General improvement suggestions to code above are along the lines of:
std::stringstream s;
s << "This is a string"; // Combine it all to 1 line, supposedly less allocations?
There is also a huge push to use the stack over heap where ever possible.
Is it possible to be preemptive about memory fragmentation in this way, or is this simply a false sense of security?
Fragmentation is already a suspected issue here, since our memory consumption increases over time.
Plain old memory leaks would be a far more likely suspect in my opinion - might want to rule those out first (and with a memory leak checker like valgrind or drmemory it's also much easier than scrutinizing your entire code-base for sources of fragmentation) – Ingrowing