Name: dos4google Date: April 25, 2008 at 09:50:07 Pacific Subject: Obtaining Memory Available OS: Win CPU/Ram: 1gb Model/Manufacturer: Dell
Comment:
Hi all I want to write a simple script to get the memory available on a machine using the mem command...Problem is mem returns a few lines of info, what is the best way to parse this and get the number that I want after typing the mem command?
Just a request for clarification: you've got Windows, with 1 GB memory. MEM is a legacy 16-bit command that runs under the Virtual DOS machine (NTVDM.) It only ever reports a maximum of 640 KB of conventional memory and 1 MB (not 1 GB) of extended memory. Is this really the value you want to obtain, or are you looking for the amount of free memory available to Windows?
More precisely mem reports up to 16 MB of extended and expanded memory, not just 1 MB. So stated, klint is right in pointing out mem is a legacy command bound to 16 bit memory addressing segmented architecture.
Hi thanks for the response - I really need to find out how much memory is available on the machine to run an application..Eg a script runs that works out that the machine has a total of 1gb RAM available and then accordingly sets a variable for the application to know to use half of the memory available on the machine e.g 512mb....
I hope this makes things clearer, any advice would be appreciated
So this is the type of problem VBScript with WMI excels at.
But it's not perfect: WMI needs to be running. This isn't normally a problem, but you never know how people modify their startup services. Also, this counts the total MB of each RAM module and adds them up, so it doesn't account for onboard devices stealing system memory. For instance, this PC has two 256MB chips in it, so the script reports 512MB; System Properties reports 504MB of RAM. Apparently, 8MB is reserved for the onboard video.
With that out of the way, this is a 6 line VBScript. If you're running it through cscript, it'll print the total MB of RAM. If not, it won't. Either way, the script will set the ERRORLEVEL to whatever it found. This allows you to do something like this from a command script:
totMem.vbs SET /A halfMem=%ERRORLEVEL% / 2 yourApp -mem %halfMem%
Why? Because I'm really just that nice and it was one extra line.
Script follows:
For Each o In GetObject("winmgmts:").InstancesOf("Win32_PhysicalMemory") mem = mem + CDbl(o.Capacity) / 2 ^ 20 Next On Error Resume Next WScript.StdOut.WriteLine mem WScript.Quit mem
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE