How to check available memory (RAM) via batch script?

…衆ロ難τιáo~ 提交于 2020-01-02 07:28:06

问题


I would like to know that how we can check the available memory in batch script? Is there any method already available? If it is not possible in batch script then is there any other way by which we can get the memory available?

OS: Windows XP / Windows 7


回答1:


This site has a sample VBScript that retrieves the total amount of memory:

http://www.computerperformance.co.uk/vbscript/wmi_memory.htm

It can be adapted to report the available amount of memory:

' 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

You can run it by saving it to a file with extension .vbs (e.g. memory.vbs) and running it with cscript.exe, e.g.:

cscript.exe //nologo memory.vbs

...to get output like:

Available memory bytes: 4481511424



回答2:


Alternative:

C:\>wmic os get freephysicalmemory
FreePhysicalMemory
4946576

to parse out to a variable (wmic output has a header + extra line on the end)

for /f "skip=1" %%p in ('wmic os get freephysicalmemory') do ( 
  set m=%%p
  goto :done
)
:done
echo free: %m%

free: 4948108

(freevirtualmemory is available also)




回答3:


Not sure about Windows XP but in Windows 7 you could use the systeminfo (external) command, as per this ServerFault question. Except on my computer that command displayed way too much information, so here's how you could limit it to the relevant part only:

systeminfo | find "Physical Memory"

The above displays the following bits of information:

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

If you want just the Available line, make your search more specific:

systeminfo | find "Available Physical Memory"



回答4:


WMIC is not available on Home/Basic/Starter editions of Windows .SYSTEMINFO is too slow. Alternative with MSHTA that should work on every windows system:

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%

And for fulness one more way with 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"



回答5:


This show the available memory for batch scripts and programs:

>mem | find "total"
    655360 bytes total conventional memory
   1048576 bytes total contiguous extended memory

Type MEM /? for further details

EDIT: Answer to new comment

>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



回答6:


This should work:

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

That'll get you the free memory. If you want the total, get the first column ($1).




回答7:


I realize this is an old post, though When I used the "WMIC OS get FreePhysicalMemory" command, the last value was a blank line. So, since I knew the second line value was what I wanted, I just used the find command add line number flag to grab the value for the second line:

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%


来源:https://stackoverflow.com/questions/11343190/how-to-check-available-memory-ram-via-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!