简体   繁体   English

我正在尝试获取总安装的RAM

[英]I am trying to get the total installed ram

I am trying to get the total installed memory. 我正在尝试获取安装的总内存。 I have 6GB installed but this is returning 5.47GB. 我安装了6GB,但是返回的是5.47GB。 What can I do to fix this? 我该怎么做才能解决此问题? I did a build on a x64 PC and am running the app on a x64 PC. 我在x64 PC上进行了构建,并且正在x64 PC上运行该应用程序。

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
            internal class MEMORYSTATUSEX
            {
                public uint dwLength;
                public uint dwMemoryLoad;
                public ulong ullTotalPhys;
                public ulong ullAvailPhys;
                public ulong ullTotalPageFile;
                public ulong ullAvailPageFile;
                public ulong ullTotalVirtual;
                public ulong ullAvailVirtual;
                public ulong ullAvailExtendedVirtual;

                public MEMORYSTATUSEX()
                {
                    this.dwLength = (uint)Marshal.SizeOf(typeof(NativeMethods.MEMORYSTATUSEX));
                }
            }

            [return: MarshalAs(UnmanagedType.Bool)]
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            internal static extern Boolean GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

public static String GetTotalRam
            {
                get
                {
                    ulong installedMemory = 0;
                    NativeMethods.MEMORYSTATUSEX memStatus = new NativeMethods.MEMORYSTATUSEX();
                    if (NativeMethods.GlobalMemoryStatusEx(memStatus))
                    {
                        installedMemory = memStatus.ullTotalPhys;
                    }
                    return ConvertBytes(installedMemory);
                }
            }

Your posted method gives you the total available memory, which is not exactly the same as the total installed memory. 您发布的方法将为您提供可用内存,这与安装的总内存不完全相同。

To get the amount of installed memory, you can use a call to the GetPhysicallyInstalledSystemMemory function. 要获取已安装的内存量,可以使用对GetPhysicallyInstalledSystemMemory函数的调用。

I think you'll find the Remarks section from that link interesting: 我认为您会从该链接中找到“ 备注”部分,这很有趣:

The GetPhysicallyInstalledSystemMemory function retrieves the amount of physically installed RAM from the computer's SMBIOS firmware tables. GetPhysicalInstalledSystemMemory函数从计算机的SMBIOS固件表中检索物理安装的RAM数量。 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. 这可能与GlobalMemoryStatusEx函数报告的数量不同,后者将MEMORYSTATUSEX结构的ullTotalPhys成员设置为操作系统可以使用的物理内存量。 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. 操作系统可用的内存量可能少于计算机上物理安装的内存量,因为BIOS和某些驱动程序可能会将内存保留为内存映射设备的I / O区域,从而使操作系统无法使用内存和应用程序。

EDIT: Added Sample code 编辑:添加了示例代码

Modified from the code found here : 此处找到的代码修改:

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetPhysicallyInstalledSystemMemory(out long TotalMemoryInKilobytes);

static void Main()
{
    long memKb;
    GetPhysicallyInstalledSystemMemory(out memKb);
    Console.WriteLine((memKb / 1024 / 1024) + " GB of RAM installed.");
}

尽管这看起来很奇怪,但添加了对Microsoft.VisualBasic.dll的引用并使用此命令:

return new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM