I’m debugging an out-of-memory exception. When I get the exception, the ‘virtual bytes’ performance counter indicates plenty of addressable space. The problem, however, is that the addressable space is badly fragmented, and the ‘Largest free region’ (returned from !address in WinDbg) is too small.
To measure the memory fragmentation, I would like to monitor the ‘Largest free region’ in perfmon. Is there a performance counter that gives me this value?
I don’t believe that there’s a single performance counter for this piece of information, but it can be deduced by using the
VirtualQueryExWin32 function.You can call it on the minimum valid virtual address (which can be obtained from
GetSystemInfo), you can then use the size of the returned page range to determine the base address of the next page range for which to callVirtualQueryEx.By walking through the address space with repeated calls to
VirtualQueryExlike this you can determine the largest page range of type MEM_FREE and what its base address is.This is the technique that I used for my ‘Address Space Monitor’ program.