The MASM Forum

General => The Laboratory => Topic started by: dedndave on October 26, 2015, 04:21:53 AM

Title: Total System RAM Estimation
Post by: dedndave on October 26, 2015, 04:21:53 AM
on this one, i am using GlobalMemoryEx to get memory information
the MEMORYSTATUSEX structure returns a "ullTotalPhys" value
but, it's never quite what you really have
(BIOS, OS, and/or software reserves some RAM, leaving the value a little short)

so, i am trying to estimate the actual total physical memory....

           MemoryLoad =          0000001Dh = 29
          GuessedPhys = 00000000_80000000h = 2147483648
            TotalPhys = 00000000_7F6BA000h = 2137759744
            AvailPhys = 00000000_5A191000h = 1511591936
        TotalPageFile = 00000000_D5193000h = 3575197696
        AvailPageFile = 00000000_B8DEA000h = 3101597696
         TotalVirtual = 00000000_7FFE0000h = 2147352576
         AvailVirtual = 00000000_7F9E2000h = 2141069312
AvailExtendedVirtual = 00000000_00000000h = 0


Title: Re: Total System RAM Estimation
Post by: Vortex on October 26, 2015, 05:14:22 AM
Hi Dave,

Nice work. A small suggestion, could you convert the byte values to megabyte? It's easier to follow and read MB values.

My test result :


           MemoryLoad =          00000023h = 35
          GuessedPhys = 00000000_80000000h = 2147483648
            TotalPhys = 00000000_7FEBB000h = 2146152448
            AvailPhys = 00000000_5257B000h = 1381478400
        TotalPageFile = 00000000_D65C6000h = 3596378112
        AvailPageFile = 00000000_926D8000h = 2456649728
         TotalVirtual = 00000000_7FFE0000h = 2147352576
         AvailVirtual = 00000000_7F9A8000h = 2140831744
AvailExtendedVirtual = 00000000_00000000h = 0
Title: Re: Total System RAM Estimation
Post by: dedndave on October 26, 2015, 06:15:06 AM
thanks Erol
i plan on doing that - but i wanted to test this little algo on different machines to see if it's ok   :P

i guess i could just run a bunch of numbers through it - lol
Title: Re: Total System RAM Estimation
Post by: TouEnMasm on October 26, 2015, 06:19:39 AM

This one is very well known on the forum,search for short memory info
Work with Windows 10
Title: Re: Total System RAM Estimation
Post by: Siekmanski on October 26, 2015, 08:08:08 AM
Windows 8.1


           MemoryLoad =          00000010h = 16
          GuessedPhys = 00000004_00000000h = 17179869184
            TotalPhys = 00000003_FC55E000h = 17118388224
            AvailPhys = 00000003_57253000h = 14346956800
        TotalPageFile = 00000004_9455E000h = 19668525056
        AvailPageFile = 00000003_D9B7A000h = 16537591808
         TotalVirtual = 00000000_7FFE0000h = 2147352576
         AvailVirtual = 00000000_7F5E2000h = 2136875008
AvailExtendedVirtual = 00000000_00000000h = 0

Press any key to continue ...
Title: Re: Total System RAM Estimation
Post by: Zen on October 28, 2015, 08:27:46 AM
Hi, DAVE,
Windows Seven Professional

          MemoryLoad =          00000032h = 50
          GuessedPhys = 00000000_80000000h = 2147483648
          TotalPhys = 00000000_7F4F8000h       = 2135916544
          AvailPhys = 00000000_3E755000h       = 1047875584
          TotalPageFile = 00000000_FE9F0000h  = 4271833088
          AvailPageFile = 00000000_ACE8B000h = 2900930560
          TotalVirtual = 00000000_7FFE0000h    = 2147352576
          AvailVirtual = 00000000_7F473000h    = 2135371776

...I'm wondering exactly what we're talking about here,...
I was playing around with WbemTest (https://technet.microsoft.com/en-us/library/cc180684.aspx) (on my computer, it is located at: C:\Windows\System32\wbem\wbemtest.exe), and trying to find a WMI class that was comparable in terms of capacity with the data that your app is producing.
See: Computer System Hardware Classes, MSDN (https://msdn.microsoft.com/en-us/library/aa389273(v=vs.85).aspx)
My two best guesses are: Win32_PhysicalMemory Class, MSDN (https://msdn.microsoft.com/en-us/library/aa394347(v=vs.85).aspx), and, Win32_MemoryDevice Class, MSDN (https://msdn.microsoft.com/en-us/library/aa394197(v=vs.85).aspx). 
When I examine the instances of both classes, there is very little useful information available. But, the instances of the Win32_PhysicalMemory Class (there are two), identify the attribute: MemoryType as DDR2. It's capacity is: 1073741824 bytes (fairly close to, AvailPhys). 
Title: Re: Total System RAM Estimation
Post by: dedndave on October 28, 2015, 09:22:13 AM
it isn't rocket science - lol

as you know, the value reported by GlobalMemoryStatusEx is always just a little bit short of actual

values like 2 MB, 4 MB, 1 GB, 2 GB, 4 GB all have one thing in common
they have a single bit set - all other bits are 0's

well, we want to accomodate the oddball cases: 3GB, 1.5 GB, etc
these all have 2 adjacent bits set

going one step further, values like 7GB seem possible, in rare cases
3 adjacent bits set

so - the algo finds the highest bit, and rounds up so that only the upper 3 bits may be set
at least, that's the idea behind it - i am going to have to re-visit this and verify my algo is correct
Title: Re: Total System RAM Estimation
Post by: Zen on October 28, 2015, 09:33:48 AM
...Ha,...Ha,...:bgrin:
I just found it a bit confusing,...
...But, seriously,...you have loyal fans here,...you could just throw in a Device Context,...and, we'd all be happy as hell,...:bgrin:
Title: Re: Total System RAM Estimation
Post by: Zen on October 29, 2015, 06:05:25 AM
Hi, DAVE, again,
You might find this SysInternals Utility interesting: RAMMap v1.4, By Mark Russinovich (https://technet.microsoft.com/en-us/sysinternals/ff700229).
...RAMMap has to run elevated, because 'it goes deep into the kernel',...
...As I recall,...you are still running on Windows XP (SO RETRO !!!),...RAM Map only works on Windows Vista and higher,...:bgrin:

...There is, however, an excellent video that explains how RAMMap works: Defrag Tools: #6 - RAMMap, Channel 9, 2012 (https://channel9.msdn.com/Shows/Defrag-Tools/Defrag-Tools-6-RAMMap).
...This video is over 30 minutes long, but, VERY informative,...

...By the way, when I run RAMMap, it shows that I have 2,085,856 KB of Total RAM Memory,...
...You might find this interesting: Working Set, MSDN (https://msdn.microsoft.com/en-us/library/windows/desktop/cc441804(v=vs.85).aspx)
Title: Re: Total System RAM Estimation
Post by: TWell on October 29, 2015, 07:40:46 AM
Something here (http://www.remkoweijnen.nl/blog/2009/03/20/reading-physical-memory-size-from-the-registry/) too
Keywords are Win32 API GetSystemFirmwareTable() and smbios
Title: Re: Total System RAM Estimation
Post by: dedndave on October 29, 2015, 12:56:22 PM
i suppose the WMI method might be best
it's probably where the System (My Computer) Properties sheet gets the value
Title: Re: Total System RAM Estimation
Post by: dedndave on October 30, 2015, 04:28:26 AM
crap - lol
Title: Re: Total System RAM Estimation
Post by: dedndave on November 07, 2015, 03:54:23 AM
2 tough ones left: OS and CPU identification (oh - and an easy one to display the hardware strings)
i've got a good start on both of those, too   :biggrin:

(http://i1154.photobucket.com/albums/p527/DednDave/dTestLib.png)
Title: Re: Total System RAM Estimation
Post by: Grincheux on December 18, 2015, 06:17:56 AM
What is the difference with GetPhysicallyInstalledSystemMemory (https://msdn.microsoft.com/en-us/library/windows/desktop/cc300158%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396)

QuoteBOOL WINAPI GetPhysicallyInstalledSystemMemory(_Out_ PULONGLONG TotalMemoryInKilobytes);

Parameters
TotalMemoryInKilobytes [out]
A pointer to a variable that receives the amount of physically installed RAM, in kilobytes.

Return value

If the function succeeds, it returns TRUE and sets the TotalMemoryInKilobytes parameter to a nonzero value.
If the function fails, it returns FALSE and does not modify the TotalMemoryInKilobytes parameter. To get extended error information, use the GetLastError function. Common errors are listed in the following table.


(http://www.phrio.biz/download/MyPc.jpg)
Title: Re: Total System RAM Estimation
Post by: dedndave on December 18, 2015, 08:23:30 AM
that may be a good method but, according to MSDN, it requires Vista +SP1 or newer
i prefer a function that works on older OS's, is all
i have kind of given up trying to support win95, or even win98
but, it's nice if it works on win2000 and newer
Title: Re: Total System RAM Estimation
Post by: npnw on February 19, 2016, 06:54:00 AM
Dave,

These are probably calling a bios routine. Then subtracting the reserved space.

Here is a link to some of the pheonix bios stuff.  This would give you a more accurate byte count of memory.

http://www.google.ch/patents/US6697920 (http://www.google.ch/patents/US6697920)

if you go down a ways, it will show you the umb memory block reserved space. You would then take the total memory, subtract all the UMB and this would give you memory above 1 mb. Then you would have to do something for memory allocation below, 1 mb, don't remember where that is, but you get the picture.   They would allocate below 1 mb, then move it to above 1 mb after booting into ____ mode on the processor.  NOt sure if they cleaned that up below the 1 mb limit. 

Hope this helps.

NPNW
Title: Re: Total System RAM Estimation
Post by: fearless on February 19, 2016, 12:09:31 PM
Had a look at these videos from Mark Russinovich from that channel9 website:

Mysteries of Memory Management Revealed,with Mark Russinovich (Part 1 of 2): https://channel9.msdn.com/events/TechEd/NorthAmerica/2011/WCL405
Mysteries of Memory Management Revealed,with Mark Russinovich (Part 2 of 2): https://channel9.msdn.com/events/TechEd/NorthAmerica/2011/WCL406

Quite informative and gives a good overview of his vmmap and rammap tools