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

8

5

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

Adabelle answered 5/7, 2012 at 11:15 Comment(1)
Based on your use of the term "batch script", should we assume you're wanting to do this on Windows? Additional information about your context and environment would help...Enabling
E
7

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
Enabling answered 5/7, 2012 at 11:42 Comment(7)
Thanks a lot, this is locale independent :)Adabelle
This is locale independent but I guess it will not work on 64 bit Win 7 OS, right? What would be the query for 64 bit os?Adabelle
Works fine on my Win7 x64 & its not locale dependentPyrometer
@Enabling may be because of this query (Select * from Win32_PerfFormattedData_PerfOS_Memory) I interpreted it will not work. As I thought for 64 bit machines it will be Win64_... something.Adabelle
@AlexK. oh great! then this is the solution for me... thanx to reubenAdabelle
@Enabling How can I find the installed RAM from the entry object?Adabelle
@Adabelle I don't see it available from the Win32_PerfFormattedData_PerfOS_Memory class, but it is available through other WMI interfaces. If you need to know this too, you should ask it as a separate question so we can provide a separate answer...Enabling
P
14

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)

Pyrometer answered 5/7, 2012 at 11:49 Comment(2)
that means wmic can only be run by a user with local admin privsPyrometer
I prefer TotalVisibleMemorySize. One may choose what they want through wmic os get.Rashid
E
7

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
Enabling answered 5/7, 2012 at 11:42 Comment(7)
Thanks a lot, this is locale independent :)Adabelle
This is locale independent but I guess it will not work on 64 bit Win 7 OS, right? What would be the query for 64 bit os?Adabelle
Works fine on my Win7 x64 & its not locale dependentPyrometer
@Enabling may be because of this query (Select * from Win32_PerfFormattedData_PerfOS_Memory) I interpreted it will not work. As I thought for 64 bit machines it will be Win64_... something.Adabelle
@AlexK. oh great! then this is the solution for me... thanx to reubenAdabelle
@Enabling How can I find the installed RAM from the entry object?Adabelle
@Adabelle I don't see it available from the Win32_PerfFormattedData_PerfOS_Memory class, but it is available through other WMI interfaces. If you need to know this too, you should ask it as a separate question so we can provide a separate answer...Enabling
H
6

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"
Histoid answered 5/7, 2012 at 11:40 Comment(2)
I am sorry to un-accept this answer because on a different locale machine (other than EN) it will fail... any generic way there?Adabelle
@Anand: Probably not. You'd need to change the search term to the one used in the output of systeminfo for that locale. Sorry. I think, as a generic and single-command solution, @Alex K.'s should do for you then (although @reuben's seems to work too).Histoid
B
5

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"
Blockade answered 28/1, 2014 at 13:3 Comment(0)
S
3

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
Supposing answered 5/7, 2012 at 20:20 Comment(4)
Hey, why have you got 599312 largest executable program size? I've only got 598832! :) Seriously, though, I think you should probably change the "for batch scripts and programs" line to read "for batch scripts and DOS programs" (or maybe "… and some legacy programs").Histoid
Some Windows 7 machines do have and some do not have that command. Why it might be so?Disbursement
@RolandPihlakas: I don't know. You may search the web looking for answers like this one, or this one, or this one, or ...Supposing
@Supposing So the answer might be "Available only in 32 bit systems".Disbursement
S
2

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).

Skurnik answered 5/7, 2012 at 11:19 Comment(0)
U
1

I found a way using below script, hope it can help. This will calculate the percentage as well

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%%%

Output would look something like this

::TotalPhysicalMemory: 262 GB
::AvailablePhysicalMemory: 240 GB
::Memory usage: 8%
Ultrared answered 17/4 at 21:4 Comment(0)
C
0

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%
Carlettacarley answered 24/8, 2018 at 2:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.