VB 获取系统内存信息
2016-02-06 21:00
417 查看
可以获取的内存信息:
- 物理内存大小
- 可用物理内存
- 物理内存占用率
- 页面内存总大小
- 可用页面内存
- 页面内存占用率
- 页面内存池大小
- 非页面内存池大小
- 虚拟内存大小
- 可用虚拟内存
- 虚拟内存占用率
代码如下(模块):
- 物理内存大小
- 可用物理内存
- 物理内存占用率
- 页面内存总大小
- 可用页面内存
- 页面内存占用率
- 页面内存池大小
- 非页面内存池大小
- 虚拟内存大小
- 可用虚拟内存
- 虚拟内存占用率
代码如下(模块):
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 St 4000 atus = 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
相关文章推荐
- IE7降低内存和降低CPU的几个技巧
- VB简单实现防止文件被改写
- VB获取文件大小的方法
- VB实现的《QQ美女找茬游戏》作弊器实例
- VB实现禁用任务管理器的方法
- VBS教程:运算符-运算符(+)
- VB实现的16位和32位md5加密代码分享
- VB6中的LSet语句和RSet语句详解
- VB实现的递归复制文件和搜索文件的代码分享
- 如何高效的使用内存
- DOS下内存的配置
- XP/win2003下发现1G的内存比512M还慢的解决方法
- VB键盘鼠标无动作调用程序的尝试
- VB XMLHTTP使用方法
- VB语言使用ADO连接、操作SQLServer数据库教程
- VB使用ADO操作Access数据库
- VB使用shell函数打开外部exe程序的实现方法
- VB的TextBox文本框实现垂直居中显示的方法
- VB关机恶搞小程序
- VB返回记录集结果到HTML表格的方法