以编程方式告诉我的VB6应用程序使用了多少内存?

时间:2013-03-04 15:57:15

标签: memory vb6 instrumentation

我有一个用VB6编写的程序,我希望它能够知道它本身使用了多少内存。

谷歌搜索只引导我进入“GlobalMemoryStatusEx”Windows API函数,但这似乎不是我正在寻找的:它可以用来提供有关计算机内存整体的信息,而我想了解当前流程本身的信息。

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:3)

您可以使用GetProcessMemoryInfo()功能:

声明:

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

致电代码:

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

答案 1 :(得分:0)

this forum thread中有一个涉及tasklist.exe shell命令的建议,该命令打印出所有正在运行的进程以及内存使用情况。我不确定性能..