Are there any good memory fragmentation profilers? (linux gcc version would be nice). Valgrind cannot analyze this because it uses custom malloc/free functions.
Thanks, Andrew
You can measure fragmentation directly by looking at /proc/buddyinfo which will tell you how many free chunks are available on each zone of each NUMA node per each order. You can do a little math and calculate what percentage of free memory is available for allocations of a certain order.
Definition. Fragmentation of memory is a type of memory disruption pertaining to the flaws or irregularities in sequences of memories, "coherence, and content” in the narrative or story of the event. During a traumatic experience, memories can be encoded irregularly which creates imperfections in the memory.
Reducing the number of sizes between these extremes also helps. Employing sizes that increase logarithmically saves a lot of fragmentation. For example, each size could be 20% larger than the previous size. “One size fits all” might not be true for memory allocators in embedded system.
In general c++ programing there is no problem with memory fragmentation. You always see the virtual memory and you always allocate contiguous virtual memory chunks. The only thing that you can notice that the sequentially allocated chunks are not necessarily adjacent in memory.
I would start with mtrace. When you have a trace, glibc comes with a perl script mtrace(1) which finds leaks. However, the trace format is easy to understand, so it should be straight-forward process this into fragmentation analysis.
I'm afraid the answer is Valgrind.
You can tell Valgrind which functions are used to make allocations and how it does it using valgrind extensions to code (so you need to modify and recompile your application, but the changes compile to noops if you're not debugging), the details are in Valgrind manual Memory pools: working with custom allocators.
Once you've done this, you have two tools that allow you to diagnose your heap usage: massif and DHAT (quick reminder, Valgrind is a set of tools, it just runs the one we all know and love, Memcheck, as default).
Massif "is a heap profiler. It measures how much heap memory your program uses. This includes both the useful space, and the extra bytes allocated for book-keeping and alignment purposes. It can also measure the size of your program's stack(s), although it does not do so by default."
It can create "graphs", so it is kind of graphical:
19.63^ ### | # | # :: | # : ::: | :::::::::# : : :: | : # : : : :: | : # : : : : ::: | : # : : : : : :: | ::::::::::: # : : : : : : ::: | : : # : : : : : : : :: | ::::: : # : : : : : : : : :: | @@@: : : # : : : : : : : : : @ | ::@ : : : # : : : : : : : : : @ | :::: @ : : : # : : : : : : : : : @ | ::: : @ : : : # : : : : : : : : : @ | ::: : : @ : : : # : : : : : : : : : @ | :::: : : : @ : : : # : : : : : : : : : @ | ::: : : : : @ : : : # : : : : : : : : : @ | :::: : : : : : @ : : : # : : : : : : : : : @ | ::: : : : : : : @ : : : # : : : : : : : : : @ 0 +----------------------------------------------------------------------->KB 0 29.48 Number of snapshots: 25 Detailed snapshots: [9, 14 (peak), 24]
What's more, there's a Massif Visualizer that is produces really pretty graphs.
DHAT allows you to diagnose how exactly the application uses its heap, which parts are short lived, which are kept through whole program's life, but used only in the beginning, etc. Unfortunately it doesn't have any nice graphs or graphical tools that go with it, the output is pure text. Thankfully you can tell it how much data you want to get and how to sort it so it's not as bad as it sounds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With