VB 获取系统内存信息
可以获取的内存信息:
- 物理内存大小
- 可用物理内存
- 物理内存占用率
- 页面内存总大小
- 可用页面内存
- 页面内存占用率
- 页面内存池大小
- 非页面内存池大小
- 虚拟内存大小
- 可用虚拟内存
- 虚拟内存占用率
代码如下(模块):
Option Explicit
Private Declare Function NtQueryInformationProcess Lib "ntdll.dll" (ByVal ProcessHandle As Long, ByVal ProcessInformationClass As Long, ByVal ProcessInformation As Long, ByVal ProcessInformationLength As Long, ByRef ReturnLength As Long) As Long
Private Declare Function NtQuerySystemInformation Lib "ntdll.dll" (ByVal SystemInformationClass As Long, ByVal SystemInformation As Long, ByVal SystemInformationLength As Long, ByRef ReturnLength As Long) As Long
Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
Private Type PROCESS_QUOTA_LIMITS
PagedPoolLimit As Long
NonPagedPoolLimit As Long
MinimumWorkingSetSize As Long
MaximumWorkingSetSize As Long
PagefileLimit As Long
TimeLimit As LARGE_INTEGER
Unknown As Long
End Type
Private Type SYSTEM_BASIC_INFORMATION
Reserved As Long
TimerResolution As Long
PageSize As Long
NumberOfPhysicalPages As Long
LowestPhysicalPage As Long
HighestPhysicalPage As Long
AllocationGranularity As Long
MinimumUserModeAddress As Long
MaximumUserModeAddress As Long
ActiveProcessorsAffinityMask As Long
NumberOfProcessors As Byte
End Type
Private Type VM_COUNTERS
PeakVirtualSize As Long
VirtualSize 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 Type MEMORY_USAGE
LoadedMemory As Long "物理内存占用率
PhysicalMemorySize As Currency "物理内存大小
AvailablePhysicalMemory As Currency "可用物理内存
PagedPoolSize As Currency "页面内存池大小
NonPagedPoolSize As Currency "非页面内存池大小
PagefileMemorySize As Currency "页面内存大小
AvailablePagefileMemory As Currency "可用页面内存大小
VirtualMemorySize As Currency "虚拟内存大小(一般为2GB)
AvailableVirtualMemory As Currency "可用虚拟内存
End Type
Public Function GetMemoryUsage(MemoryUsage As MEMORY_USAGE) As Long
Dim BasicInfo As SYSTEM_BASIC_INFORMATION
Dim QuotaLimits As PROCESS_QUOTA_LIMITS
Dim Vm As VM_COUNTERS
Dim PerInfo(77) As Long
Dim Status As Long
Dim ReturnLength As Long
Dim ProcessAPagefile As Currency
Dim SystemAPagefile As Currency
Status = NtQuerySystemInformation(0, ByVal VarPtr(BasicInfo), LenB(BasicInfo), 0)
If Status Then Exit Function
Status = NtQuerySystemInformation(2, ByVal VarPtr(PerInfo(0)), 312, 0)
If Status Then Exit Function
Status = NtQueryInformationProcess(-1, 1, ByVal VarPtr(QuotaLimits), Len(QuotaLimits), ReturnLength)
Status = NtQueryInformationProcess(-1, 3, ByVal VarPtr(Vm), LenB(Vm), 0)
If PerInfo(11) < 100 Then
MemoryUsage.LoadedMemory = 100
Else
MemoryUsage.LoadedMemory = Fix((BasicInfo.NumberOfPhysicalPages - PerInfo(11)) * 100 / BasicInfo.NumberOfPhysicalPages)
End If
MemoryUsage.PhysicalMemorySize = CCur(BasicInfo.NumberOfPhysicalPages) * BasicInfo.PageSize
MemoryUsage.AvailablePhysicalMemory = CCur(PerInfo(11)) * BasicInfo.PageSize
MemoryUsage.PagedPoolSize = CCur(PerInfo(28)) * BasicInfo.PageSize
MemoryUsage.NonPagedPoolSize = CCur(PerInfo(29)) * BasicInfo.PageSize
If PerInfo(13) < QuotaLimits.PagefileLimit Then
MemoryUsage.PagefileMemorySize = CCur(QuotaLimits.PagefileLimit) * BasicInfo.PageSize
Else
MemoryUsage.PagefileMemorySize = CCur(PerInfo(13)) * BasicInfo.PageSize
End If
MemoryUsage.VirtualMemorySize = BasicInfo.MaximumUserModeAddress - BasicInfo.MinimumUserModeAddress + 1
MemoryUsage.AvailableVirtualMemory = MemoryUsage.VirtualMemorySize - Vm.VirtualSize
SystemAPagefile = PerInfo(13) - PerInfo(12)
ProcessAPagefile = QuotaLimits.PagefileLimit - Vm.PagefileUsage
Status = Fix(MemoryUsage.AvailablePhysicalMemory / MemoryUsage.PhysicalMemorySize * 100)
If SystemAPagefile > ProcessAPagefile Then
MemoryUsage.AvailablePagefileMemory = CCur(SystemAPagefile) * BasicInfo.PageSize
Else
MemoryUsage.AvailablePagefileMemory = CCur(ProcessAPagefile) * BasicInfo.PageSize
End If
End Function
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: vb.net 获取CPU使用率
- 下一篇: windows平台一个程序究竟占用多少内存?