public static HeapAllocationSizes[] GetProcessHeapSizes() { var count = GetProcessHeaps(0, null); var buffer = new IntPtr[count]; GetProcessHeaps(count, buffer); var sizes = new HeapAllocationSizes[count]; for (int i = 0; i < count; i++) { var h = buffer[i]; HeapLock(h); var e = new PROCESS_HEAP_ENTRY(); while (HeapWalk(h, ref e)) { if ((e.wFlags & PROCESS_HEAP_ENTRY_WFLAGS.PROCESS_HEAP_ENTRY_BUSY) != 0) { sizes[i].Committed += e.cbData + e.cbOverhead; } else if ((e.wFlags & PROCESS_HEAP_ENTRY_WFLAGS.PROCESS_HEAP_UNCOMMITTED_RANGE) != 0) { sizes[i].Reserved += e.cbData + e.cbOverhead; } else { sizes[i].Unknown += e.cbData + e.cbOverhead; } } HeapUnlock(h); } return(sizes); }
public static HeapAllocationSizes[] GetProcessHeapSizes(string dmpDir) { if (HeapDiagnostics && dmpDir != null) { SizeTracker.NextDump(); TRACK_SIZES.ForEach(t => t.OpenDumpFile(dmpDir)); } var count = GetProcessHeaps(0, null); var buffer = new IntPtr[count]; GetProcessHeaps(count, buffer); var sizes = new HeapAllocationSizes[count]; for (int i = 0; i < count; i++) { var committedSizes = HeapDiagnostics ? new Dictionary <long, int>() : null; var stringCounts = HeapDiagnostics ? new Dictionary <string, int>() : null; var h = buffer[i]; HeapLock(h); var e = new PROCESS_HEAP_ENTRY(); while (HeapWalk(h, ref e)) { if ((e.wFlags & PROCESS_HEAP_ENTRY_WFLAGS.PROCESS_HEAP_ENTRY_BUSY) != 0) { sizes[i].Committed += e.cbData + e.cbOverhead; if (committedSizes != null) { // Update count if (!committedSizes.ContainsKey(e.cbData)) { committedSizes[e.cbData] = 0; } committedSizes[e.cbData]++; } if (stringCounts != null) { // Find string(s) var byteData = new byte[e.cbData]; IntPtr pData = e.lpData; Marshal.Copy(pData, byteData, 0, byteData.Length); TRACK_SIZES.ForEach(t => t.DumpUnseenSize(pData, byteData)); foreach (var byteString in FindStrings(byteData)) { if (!stringCounts.ContainsKey(byteString)) { stringCounts[byteString] = 0; } stringCounts[byteString]++; } } } else if ((e.wFlags & PROCESS_HEAP_ENTRY_WFLAGS.PROCESS_HEAP_UNCOMMITTED_RANGE) != 0) { sizes[i].Reserved += e.cbData + e.cbOverhead; } else { sizes[i].Unknown += e.cbData + e.cbOverhead; } } HeapUnlock(h); if (committedSizes != null) { sizes[i].CommittedSizes = committedSizes.OrderByDescending(p => p.Value).ToList(); } if (stringCounts != null) { sizes[i].StringCounts = stringCounts.OrderByDescending(p => p.Value).ToList(); } } if (HeapDiagnostics) { TRACK_SIZES.ForEach(t => t.CloseDumpFile()); } return(sizes); }