我有一个用 VB6 编写的程序,我希望它能够知道它本身正在使用多少内存。
谷歌搜索了一下只让我找到了“GlobalMemoryStatusEx”Windows API 函数,但这似乎不是我想要的:它可以用来提供有关计算机整体内存的信息,而我想要关于当前流程本身。
有任何想法吗?谢谢。
我有一个用 VB6 编写的程序,我希望它能够知道它本身正在使用多少内存。
谷歌搜索了一下只让我找到了“GlobalMemoryStatusEx”Windows API 函数,但这似乎不是我想要的:它可以用来提供有关计算机整体内存的信息,而我想要关于当前流程本身。
有任何想法吗?谢谢。
您可以使用以下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
在这个论坛帖子中,有一个涉及tasklist.exe
shell 命令的建议,它会打印出所有正在运行的进程以及内存使用情况。我不确定性能..