Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically tell how much memory my VB6 app is using?

I have a program, written in VB6, and I'd like it to be able to know how much memory it itself is using.

Googling around a bit has only led me to the "GlobalMemoryStatusEx" Windows API function, but that doesn't seem to be what I'm looking for: It can be used to give information about the computer's memory overall, whereas I want information about the current process itself.

Any ideas? Thanks.

like image 914
user2132436 Avatar asked Nov 22 '25 01:11

user2132436


1 Answers

You can use the GetProcessMemoryInfo() function:

Declarations:

Public Type PROCESS_MEMORY_COUNTERS
  cb As Long
  PageFaultCount As Long
  PeakWorkingSetSize As Long
  WorkingSetSize As Long
  QuotaPeakPagedPoolUsage As Long
  QuotaPagedPoolUsage As Long
  QuotaPeakNonPagedPoolUsage As Long
  QuotaNonPagedPoolUsage As Long
  PagefileUsage As Long
  PeakPagefileUsage As Long
End Type

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
Public Declare Function GetProcessMemoryInfo Lib "PSAPI.DLL" (ByVal hProcess As Long, ppsmemCounters As PROCESS_MEMORY_COUNTERS, ByVal cb As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Calling code:

Dim tPMC As PROCESS_MEMORY_COUNTERS

'ProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcessID)
ProcessHandle = GetCurrentProcess()

If (GetProcessMemoryInfo(ProcessHandle, tPMC, Len(tPMC)) <> 0) Then
  PageFaultCount = Format(tPMC.PageFaultCount, "#,###")
  WorkingSetSize = FormatFileSize(tPMC.WorkingSetSize)
  PageFileUsage = FormatFileSize(tPMC.PagefileUsage)
End If

CloseHandle ProcessHandle
like image 136
Deanna Avatar answered Nov 24 '25 23:11

Deanna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!