detect memory leaks
Posted: Fri Oct 03, 2008 10:21 pm
I think I found this somewhere in the forum, though can't remember where but it's handy to help track down problematic memory leaks.
Code: Select all
Prototype.l GetProcessMemoryInfo_(hProcess.l, *p, cb.l)
Procedure.l GetMemoryUsage(kilobytes.b=#True)
Structure PROCESS_MEMORY_COUNTERS
cb.l;DW
PageFaultCount.l;DW
PeakWorkingSetSize.l;SIZE_T
WorkingSetSize.l;SIZE_T
QuotaPeakPagedPoolUsage.l;SIZE_T
QuotaPagedPoolUsage.l;SIZE_T
QuotaPeakNonPagedPoolUsage.l;SIZE_T
QuotaNonPagedPoolUsage.l;SIZE_T
PagefileUsage.l;SIZE_T
PeakPagefileUsage.l;SIZE_T
EndStructure
Static GetProcessMemoryInfo_.GetProcessMemoryInfo_
Static PSAPI
If Not GetProcessMemoryInfo_
If Not PSAPI
PSAPI=OpenLibrary(#PB_Any, "PSAPI.DLL")
EndIf
If PSAPI
GetProcessMemoryInfo_=GetFunction(PSAPI, "GetProcessMemoryInfo")
EndIf
EndIf
If GetProcessMemoryInfo_
Protected p.PROCESS_MEMORY_COUNTERS
GetProcessMemoryInfo_(GetCurrentProcess_(), @p, SizeOf(PROCESS_MEMORY_COUNTERS))
If kilobytes
ProcedureReturn (p\WorkingSetSize+p\PagefileUsage) /1024
Else
ProcedureReturn (p\WorkingSetSize + p\PagefileUsage)
EndIf
EndIf
EndProcedure