Tell the tale of memory fragmentation (as opposed to memory leaks)?

I realize at first that leaks can fragment memory badly, but please bear with me.

  • Using WinDbg and linking to a process: using! heap (or another WinDbg command), what should I expect, see if I'm dealing with memory fragmentation and not leaking? For example, I can use "! Heap stat" and "! Heap stat -h handle" to zero in the code that generates the leak; but is there something about these same return values ​​that hint at fragmentation?
  • Has the memory allocation been changed between XP and Vista? Specifically, what's related to DLL and other library loading? We are developing exclusively on XP, so I'm not familiar with Vista, but it turns out that some of the memory problems we saw on XP go away when we install the same files in Vista.

Thank!

+2


source to share


3 answers


I wish I could help you with the fragmentation issue, so I'm going to address the second question.



Vista introduced ASLR, which changes the way DLLs are loaded. For more information see this wiki entry and a more specific discussion of this post may be helpful.

+2


source


There are several different types of fragmentation: address space fragmentation and heap fragmentation. The former can lead to failures in heap managed or unmanaged deployments or failures in loading DLLs, the latter can lead to memory allocation failures on calls new

.

You can use !address -summary

to get an overview of the address space. This tells how much free space, committed, is used for DLL mapping, virtual address descriptors (metadata), etc. The sysinternals VMMap tool gives you a graphical view, no debugging required.

For heap fragmentation, the output from !heap -s

must contain some indication of how fragmented the unmanaged heaps are, for example:

00970000 00001002   64576  39232  49736   5732  1314   448    0      1   L  
    External fragmentation  14 % (1314 free blocks)
    Virtual address fragmentation  21 % (448 uncommited ranges)

      



You can dig into this using !heap -stat

eg. !heap -stat -h 00970000

given the above output, and that will tell you the distribution of placement sizes, etc. This can be useful if you have a large number of small objects, if you are not using a bunch of fragmentation, for example:

0:057> !heap -stat -h 00970000 
 heap @ 00970000
group-by: TOTSIZE max-display: 20
    size     #blocks     total     ( %) (percent of total busy bytes)
    134 c0c8 - e7f0a0  (50.72)
    18 ee22 - 165330  (4.88)
    8c 26f9 - 15502c  (4.66)
    a4 1ffc - 147d70  (4.48)

      

Hope it helps.

+9


source


Starting with Windows Vista, a new memory manager called the Least Fragmentation Heap (m2) is activated by default.

Description MS

On Windows XP, you can enable a bunch of fragmentation with the following code:

HANDLE heaps[1025];
DWORD nheaps = GetProcessHeaps((sizeof(heaps) / sizeof(HANDLE)) - 1, heaps);
for (DWORD i = 0; i < nheaps; ++i) {
  ULONG  enableLFH = 2;
  HeapSetInformation(heaps[i], HeapCompatibilityInformation, &enableLFH, sizeof(enableLFH));
}

      

+2


source







All Articles