我想知道如何在批处理脚本中检查可用内存?有没有现成的方法?如果在批处理脚本中不可能,那么还有其他方法可以获取可用内存吗?
操作系统:Windows XP / Windows 7
替代方案:
C:\>wmic os get freephysicalmemory
FreePhysicalMemory
4946576
解析为变量(wmic 输出有一个标题+末尾的额外行)
for /f "skip=1" %%p in ('wmic os get freephysicalmemory') do (
set m=%%p
goto :done
)
:done
echo free: %m%
free: 4948108
(也有
freevirtualmemory
)
此站点有一个示例 VBScript,用于检索总内存量:
http://www.computerperformance.co.uk/vbscript/wmi_memory.htm
它可以适应报告可用内存量:
' Memory.vbs
' Sample VBScript to discover how much RAM in computer
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - August 2010
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, perfData, entry
Dim strLogonUser, strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set perfData = objWMIService.ExecQuery _
("Select * from Win32_PerfFormattedData_PerfOS_Memory")
For Each entry in perfData
Wscript.Echo "Available memory bytes: " & entry.AvailableBytes
Next
WScript.Quit
您可以通过将其保存到扩展名为
.vbs
(例如 memory.vbs
)的文件并使用 cscript.exe 运行它来运行它,例如:
cscript.exe //nologo memory.vbs
...获得如下输出:
Available memory bytes: 4481511424
不确定 Windows XP,但在 Windows 7 中,您可以使用
systeminfo
(外部)命令,按照此 ServerFault 问题。除了在我的计算机上,该命令显示了太多信息,因此您可以按照以下方法将其限制为仅相关部分:
systeminfo | find "Physical Memory"
上面显示了以下信息:
总物理内存:n,nnn MB 可用物理内存:n,nnn MB
如果您只想要
Available
行,请使您的搜索更具体:
systeminfo | find "Available Physical Memory"
WMIC
在 Windows 的 Home/Basic/Starter 版本上不可用。SYSTEMINFO
太慢。
MSHTA
的替代方案应该适用于每个 Windows 系统:
for /f "usebackq" %%a in (`mshta ^"javascript^:close^(new ActiveXObject^(^'Scripting.FileSystemObject^'^).GetStandardStream^(1^).Write^(GetObject^(^'winmgmts:^'^).ExecQuery^(^'Select * from Win32_PerfFormattedData_PerfOS_Memory^'^).ItemIndex^(0^).AvailableBytes^)^);^"^|more`) do set free_mem=%%a
echo %free_mem%
为了更完整,使用 dxdiag 的另一种方式:
@echo off
taskkill /im dxdiag* /f
dxdiag /whql:off /t %cd%\dxdiag.txt
:ckeck_dx
tasklist | find "dxdiag" && ( w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:5 >nul 2>&1 & goto :ckeck_dx )
find "Available OS Memory:" "dxdiag.txt"
del /q /f "%~dp0dxdiag.txt"
这显示了批处理脚本和程序的可用内存:
>mem | find "total"
655360 bytes total conventional memory
1048576 bytes total contiguous extended memory
输入 MEM /?了解更多详情
编辑:回答新评论
>mem | find "avail"
655360 bytes available to MS-DOS
0 bytes available contiguous extended memory
941056 bytes available XMS memory
>mem
655360 bytes total conventional memory
655360 bytes available to MS-DOS
599312 largest executable program size
1048576 bytes total contiguous extended memory
0 bytes available contiguous extended memory
941056 bytes available XMS memory
MS-DOS resident in High Memory Area
free_mem=`free | sed -n 2p | awk '{print $4}'`
这将为您带来空闲内存。如果您想要总数,请获取第一列(1 美元)。
for /f "tokens=1,2 delims=]" %%p in ('WMIC OS get FreePhysicalMemory^|find /N /V ""') do (IF %%p equ [2 (set MEM=%%q))
echo.WMIC FreePhysicalMemory = %MEM%
for /f "tokens=2 delims==" %%a in ('wmic computersystem get
TotalPhysicalMemory /value') do (
for /f "delims=" %%b in ("%%a") do (
Set "MEM=%%b"
)
)
for /f "tokens=2 delims==" %%a in ('wmic OS get FreePhysicalMemory
/value') do (
for /f "delims=" %%b in ("%%a") do (
Set "AVMEM=%%b"
)
)
Set /a TotalPhysicalMemory = %MEM:~0,-3%/1024/1024
Set /a AvailablePhysicalMemory = %AVMEM%/1024/1024
echo TotalPhysicalMemory: %TotalPhysicalMemory% GB
echo AvailablePhysicalMemory: %AvailablePhysicalMemory% GB
set /a UsedPhysicalMemory=TotalPhysicalMemory - AvailablePhysicalMemory
set /a UsedPercent=(UsedPhysicalMemory * 100)/TotalPhysicalMemory
echo Memory usage: %UsedPercent%%%
输出看起来像这样
::TotalPhysicalMemory: 262 GB
::AvailablePhysicalMemory: 240 GB
::Memory usage: 8%