VB and VBA Users Source Code: Determining the amount of memory used by an application
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Determining the amount of memory used by an application
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, November 18, 2005
Hits:
2929
Category:
Windows API
Article:
It is often useful to be able to track an application's memory usuage, especially if you are trying to trace a memory leak. The code below returns the process memory counters for a specified process (a demo routine can be found at the bottom of the post): Option Explicit Private Declare Function GetProcessMemoryInfo Lib "PSAPI.DLL" (ByVal hProcess As Long, ppsmemCounters As PROCESS_MEMORY_COUNTERS, ByVal cb As Long) As Long Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long Public Type PROCESS_MEMORY_COUNTERS cb As Long PageFaultCount As Long 'Number of page faults. PeakWorkingSetSize As Long 'Peak working set size, in bytes. WorkingSetSize As Long 'Current working set size, in bytes. QuotaPeakPagedPoolUsage As Long 'Peak paged pool usage, in bytes. QuotaPagedPoolUsage As Long 'Current paged pool usage, in bytes. QuotaPeakNonPagedPoolUsage As Long 'Peak nonpaged pool usage, in bytes. QuotaNonPagedPoolUsage As Long 'Current nonpaged pool usage, in bytes. PagefileUsage As Long 'Current space allocated for the pagefile, in bytes. Those pages may or may not be in memory. PeakPagefileUsage As Long 'Peak space allocated for the pagefile, in bytes. End Type 'Purpose : Returns information on the memory status of the specified process. 'Inputs : [lProcessId] The process id of the process to return the memory info on. 'Outputs : Returns a structure containing the requested process memory information. 'Author : Andrew Baker 'Date : 07/09/2000 'Example : Debug.Print "Temp Files: " & FileCountMatching("C:\Temp\*.*") 'Notes: : Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation 4.0. Public Function GetProcesses(Optional lProcessId = -1) As PROCESS_MEMORY_COUNTERS Dim tProcessMem As PROCESS_MEMORY_COUNTERS Dim hProcess As Long Const PROCESS_QUERY_INFORMATION As Long = (&H400) Const PROCESS_VM_READ As Long = (&H10) On Error GoTo ErrFailed If lProcessId = -1 Then 'Get the process handle for the current process lProcessId = GetCurrentProcessId End If 'Open the process hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcessId) If hProcess Then 'Get the Site of the Memory Structure tProcessMem.cb = LenB(tProcessMem) Call GetProcessMemoryInfo(hProcess, tProcessMem, tProcessMem.cb) 'Close the handle Call CloseHandle(hProcess) 'Return the result GetProcesses = tProcessMem Else Debug.Print "Failed to open the process!" Debug.Assert False End If Exit Function ErrFailed: Debug.Print "Error in GetProcesses: " & Err.Description Debug.Assert False End Function 'Demonstration routine Sub Test() Dim tProcessMem As PROCESS_MEMORY_COUNTERS Dim lBytesPerMeg As Long tProcessMem = GetProcesses() lBytesPerMeg = CLng(1024) * CLng(1024) Debug.Print "This application's working set size: " & Format((tProcessMem.WorkingSetSize / lBytesPerMeg), "#,##0.00") & "MB" End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder