Win10下通过WMIC获取内存配置和使用信息

admin 6695 2025-09-27 00:39:46

基本原理,通过WMIC,获取memorychip和memphysical相关信息

例子1,使用MEMORYCHIP获取安装的memory信息,按bank区分

D:\>wmic MEMORYCHIP get Banklabel,DeviceLocator,MemoryType,TypeDetail,speedBankLabel DeviceLocator MemoryType Speed TypeDetailBANK 0 ChannelA-DIMM0 0 2133 128BANK 2 ChannelB-DIMM0 0 2133 128

高级例子,讲上述过程写成PYTHON程序,并利用Python的字符处理能力格式化信息

代码如下:

import os

import ctypes

Drive = os.environ['SYSTEMDRIVE']

GetRamSlots = "wmic memphysical get memorydevices"

GetRamSoltCapacity = "wmic memphysical get MaxCapacity"

GetUsedSolt = "wmic MEMORYCHIP get Banklabel,DeviceLocator,MemoryType,TypeDetail,speed"

GetFreeMemory = 'systeminfo | findstr /'+ Drive +'"Total Physical Memory" /'+Drive+'"Available Physical Memory"'

class disable_file_system_redirection:

_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection

_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection

def __enter__(self):

self.old_value = ctypes.c_long()

self.success = self._disable(ctypes.byref(self.old_value))

def __exit__(self, type, value, traceback):

if self.success:

self._revert(self.old_value)

with disable_file_system_redirection():

Run_GetRamSlots = os.popen(GetRamSlots).read()

Run_GetRamSoltCapacity = os.popen(GetRamSoltCapacity).read()

Run_GetUsedSolt = os.popen(GetUsedSolt).read()

Run_GetFreeMemory = os.popen(GetFreeMemory).read()

print("Getting RAM details.. \n")

for i in [i.strip() for i in Run_GetRamSlots.split('\n') if 'MemoryDevices' not in i if i.strip()]:

print ("The Number of RAM slots available:"+i+"\n")

for i in [i.strip() for i in Run_GetRamSoltCapacity.split('\n') if 'MaxCapacity' not in i if i.strip()]:

B = int(i)

GB = 1024*1024.0

Convert = B/GB

print ("The capacity of each RAM per slot is:",Convert , "GB")

print ("\n")

c = 0

for i in [i.strip() for i in Run_GetUsedSolt.split('\n') if 'BankLabel' not in i if i.strip()]:

c =c+1

print ("The number of used slot(s):", c)

print ("The details of the used slots:")

print (Run_GetUsedSolt)

print ("The installed RAM memory details:")

print (Run_GetFreeMemory)

print ("\n")

输出如下:

The Number of RAM slots available:4

The capacity of each RAM per slot is: 32.0 GB

The number of used slot(s): 2

The details of the used slots:

BankLabel DeviceLocator MemoryType Speed TypeDetail

BANK 0 ChannelA-DIMM0 0 2133 128

BANK 2 ChannelB-DIMM0 0 2133 128

The installed RAM memory details:

Total Physical Memory: 16,135 MB

Available Physical Memory: 5,961 MB

ref: https://scripts.comodo.com/frontend/web/topic/show-available-ram-sockets-on-the-motherboard

上一篇
下一篇
相关文章