如何通过批处理脚本检查可用内存(RAM)?

时间:2012-07-05 11:15:33

标签: windows-7 batch-file windows-xp

我想知道我们如何检查批处理脚本中的可用内存?有没有可用的方法?如果在批处理脚本中不可能,那么还有其他方法可以让内存可用吗?

操作系统:Windows XP / Windows 7

7 个答案:

答案 0 :(得分:12)

替代:

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也可用)

答案 1 :(得分:7)

此站点有一个示例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

答案 2 :(得分:6)

不确定Windows XP,但在Windows 7中,您可以按照this ServerFault question使用systeminfo(外部)命令。除了在我的计算机上,该命令显示的信息太多,所以这里只能将它限制在相关部分:

systeminfo | find "Physical Memory"

以上显示以下信息:

Total Physical Memory:     n,nnn MB
Available Physical Memory: n,nnn MB

如果您只需要Available行,请使搜索更具体:

systeminfo | find "Available Physical Memory"

答案 3 :(得分:5)

Windows的Home / Basic / Starter版本不提供

WMICSYSTEMINFO速度太慢。 替代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"

答案 4 :(得分:3)

这显示批处理脚本和程序的可用内存

>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

答案 5 :(得分:1)

这应该有效:

free_mem=`free | sed -n 2p | awk '{print $4}'`

那会让你获得免费记忆。如果你想要总数,可以得到第一列($ 1)。

答案 6 :(得分:0)

我意识到这是一篇老文章,尽管当我使用“ WMIC OS get FreePhysicalMemory”命令时,最后一个值是空白行。因此,由于我知道第二行的值是我想要的,所以我只是使用find命令添加行号标志来获取第二行的值:

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%