C#通过WinAPI获取内存信息,32位64位可用

时间:2022-03-23 17:19:19

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleGetSysMemory
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct 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;
        }

[DllImport("kernel32.dll")]
        public static extern void GlobalMemoryStatusEx(ref MEMORYSTATUSEX stat);

static void Main(string[] args)
        {
            string msg = "物理内存总数:{1}Mb{0}可用物理内存:{2}Mb{0}虚拟内存总数:{3}Mb{0}可用虚拟内存:{4}Mb";

MEMORYSTATUSEX memInfo = new MEMORYSTATUSEX();
            memInfo.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));

GlobalMemoryStatusEx(ref memInfo);

Console.WriteLine(string.Format(msg,
                Environment.NewLine,
                memInfo.ullTotalPhys / 1024 / 1024,
                memInfo.ullAvailPhys / 1024 / 1024,
                memInfo.ullTotalVirtual / 1024 / 1024,
                memInfo.ullAvailVirtual / 1024 / 1024));

Console.ReadKey();
        }
    }
}

也可以用另一个API GlobalMemoryStatus获取上述信息,但该方法参数长度在32位和64位系统下不同,如定义错误拿不到正确值,如下:

32位系统

[StructLayout(LayoutKind.Sequential)]
        public struct MemoryStatus
        {
            public uint Length;
            public uint MemoryLoad;
            public uint TotalPhysical;
            public uint AvailablePhysical;//可用物理内存
            public uint TotalPageFile;
            public uint AvailablePageFile;
            public uint TotalVirtual;
            public uint AvailableVirtual;
        }

MemoryStatus memStatus = new MemoryStatus();
            memStatus.Length = (uint)Marshal.SizeOf(typeof(MemoryStatus));

GlobalMemoryStatus(ref memStatus);

Console.WriteLine(string.Format(msg,
                Environment.NewLine,
                memStatus.TotalPhysical / 1024 / 1024,
                memStatus.AvailablePhysical / 1024 / 1024,
                memStatus.TotalVirtual / 1024 / 1024,
                memStatus.AvailableVirtual / 1024 / 1024));

64位系统

[StructLayout(LayoutKind.Sequential)]
        public struct MemoryStatus
        {
            public uint Length;
            public uint MemoryLoad;
            public ulong TotalPhysical;
            public ulong AvailablePhysical;//可用物理内存
            public ulong TotalPageFile;
            public ulong AvailablePageFile;
            public ulong TotalVirtual;
            public ulong AvailableVirtual;
        }

MemoryStatus memStatus = new MemoryStatus();
            memStatus.Length = (uint)Marshal.SizeOf(typeof(MemoryStatus));

GlobalMemoryStatus(ref memStatus);

Console.WriteLine(string.Format(msg,
                Environment.NewLine,
                (long)memStatus.TotalPhysical / 1024 / 1024,
                (long)memStatus.AvailablePhysical / 1024 / 1024,
                (long)memStatus.TotalVirtual / 1024 / 1024,
                (long)memStatus.AvailableVirtual / 1024 / 1024));