private static ulong GetAvailableMemory() { var meminfo = new Memorystatusex(); int temp; meminfo.dwLength = 64; //此方法为手动Hack,按照填充规则计算大小,祝我好运 temp = GlobalMemoryStatusEx(ref meminfo); //实践证明,必须有人接收返回值,否则会报错 return(meminfo.ullAvailPhys / 1024 / 1024); }
private static ulong GetTotalInstalledMemory() { var meminfo = new Memorystatusex(); int temp; meminfo.dwLength = 64; //此方法为手动Hack,按照填充规则计算大小,祝我好运,希望有更好的办法(但是Unsafe的sizeof算符简直令人无力吐槽) temp = GlobalMemoryStatusEx(ref meminfo); //实践证明,必须有人接收返回值,否则会报错 return(meminfo.ullTotalPhys / 1024 / 1024 / 1024); }
[DllImport("kernel32.dll", EntryPoint = "GlobalMemoryStatusEx", CallingConvention = CallingConvention.StdCall)]//此处一定要用Ex,否则内存计算不全 private static extern int GlobalMemoryStatusEx(ref Memorystatusex lpBuffer);