Windows not telling me correct amount of RAM
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Windows not telling me correct amount of RAM

  1. #1
    Join Date
    Sep 2016
    Posts
    12

    Windows not telling me correct amount of RAM

    Hello,
    I'm having a problem reporting how much RAM is installed on my servers. My code is the following:

    Code:
    MEMORYSTATUSEX memInfo;
    memInfo.dwLength = sizeof(MEMORYSTATUSEX);
    GlobalMemoryStatusEx(&memInfo);
    std::cout << (unsigned long)memInfo.ullTotalPhys / (1024*1024) << "MB" << std::endl;
    I know that my server has 8GB of RAM in it. Both Computer > Properties and the vSphere client indicate that. But, the code above tells me that there is 4GB.

    I can't use the GetPhysicallyInstalledSystemMemory function because it's not available in the legacy API. Is there a reason that I'm getting an incorrect RAM amount?

    Name:  Screenshot 2017-02-21 13.44.48.jpg
Views: 40
Size:  23.7 KB

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,120

    Re: Windows not telling me correct amount of RAM

    Because you are casting memInfo.ullTotalPhys to an unsigned long before the calculation - which is truncating the value! Try removing the cast.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. This post is public domain as specified at https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,806

    Re: Windows not telling me correct amount of RAM

    Perhaps, the reason is what MSDN mentions in Remark section of https://msdn.microsoft.com/en-us/lib...(v=VS.85).aspx :
    Remarks
    The GetPhysicallyInstalledSystemMemory function retrieves the amount of physically installed RAM from the computer's SMBIOS firmware tables. This can differ from the amount reported by the GlobalMemoryStatusEx function, which sets the ullTotalPhys member of the MEMORYSTATUSEX structure to the amount of physical memory that is available for the operating system to use. The amount of memory available to the operating system can be less than the amount of memory physically installed in the computer because the BIOS and some drivers may reserve memory as I/O regions for memory-mapped devices, making the memory unavailable to the operating system and applications.
    The amount of physical memory retrieved by the GetPhysicallyInstalledSystemMemory function must be equal to or greater than the amount reported by the GlobalMemoryStatusEx function; if it is less, the SMBIOS data is malformed and the function fails with ERROR_INVALID_DATA. Malformed SMBIOS data may indicate a problem with the user's computer.
    Victor Nijegorodov

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,806

    Re: Windows not telling me correct amount of RAM

    Quote Originally Posted by 2kaud View Post
    Because you are casting memInfo.ullTotalPhys to an unsigned long before the calculation - which is truncating the value! Try removing the cast.
    Good catch!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center