Our clients experience OOM in some specific scenarios and we can't pinpoint any memory leaks so we would like to understand if it's a memory fragmentation issue.
There are a lot of different memory metrics in both OSX and WIN (resident, dirty, working set, reserved, committed, ...) and we can't figure out which metrics we can use to measure fragmentation.
vmmap command in OSX presents a fragmentation column and it seems to be measured as follows :
(DIRTY+SWAP FRAG SIZE) = DIRTY SIZE - BYTES ALLOCATED
FRAG = (DIRTY+SWAP FRAG SIZE) / DIRTY SIZE x 100.0
The question is how we can retrieve DIRTY SIZE and BYTES ALLOCATED ?
There is mstats which gives the amount of memory allocated and used via malloc but the reported values doesn't match anything we see in vmmap.
WIN is also a totally different story. We had a look at the following values but we don't know if they can be used to measure fragmentation:
static void vminfo (unsigned long *free, unsigned long *reserved, unsigned long *committed) {
MEMORY_BASIC_INFORMATION memory_info;
memory_info.BaseAddress = 0;
*free = *reserved = *committed = 0;
while (VirtualQuery (memory_info.BaseAddress, &memory_info, sizeof (memory_info))) {
switch (memory_info.State) {
case MEM_FREE:
*free += memory_info.RegionSize;
break;
case MEM_RESERVE:
*reserved += memory_info.RegionSize;
break;
case MEM_COMMIT:
*committed += memory_info.RegionSize;
break;
}
memory_info.BaseAddress = (char *) memory_info.BaseAddress + memory_info.RegionSize;
}
}
We also had a look at GetProcessMemoryInfo and same story, we don't know if and how values from PROCESS_MEMORY_COUNTERS_EX can be used to measure fragmentation.
TLDR : How can we measure fragmentation in OSX and WIN ?


(DIRTY+SWAP) FRAG SIZE = (DIRTY+SWAP) - ALLOCATED
(DIRTY+SWAP) FRAG SIZE / (DIRTY+SWAP) x 100.0 = % FRAG
(DIRTY+SWAP) = footprint