Joe*_*oel 78 c# memory performancecounter
使用C#,我想获得我的计算机拥有的RAM总量.使用PerformanceCounter,我可以通过设置获得可用ram的数量:
counter.CategoryName = "Memory";
counter.Countername = "Available MBytes";
Run Code Online (Sandbox Code Playgroud)
但我似乎无法找到获得总内存量的方法.我该怎么做呢?
更新:
MagicKat:我在搜索时看到了它,但它不起作用 - "你错过了一个装配或参考吗?".我希望将它添加到参考文献中,但我没有在那里看到它.
Mag*_*Kat 174
添加对Microsoft.VisualBasic
和的引用using Microsoft.VisualBasic.Devices;
.
该ComputerInfo
课程包含您需要的所有信息.
Phi*_*eck 59
p/invoke方式编辑:更改为GlobalMemoryStatusEx以提供准确的结果(呵呵)
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private 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)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
Run Code Online (Sandbox Code Playgroud)
然后使用像:
ulong installedMemory;
MEMORYSTATUSEX memStatus = new MEMORYSTATUSEX();
if( GlobalMemoryStatusEx( memStatus))
{
installedMemory = memStatus.ullTotalPhys;
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用WMI(托管但速度较慢)来查询"Win32_ComputerSystem"类中的"TotalPhysicalMemory".
从joel-llamaduck.blogspot.com 编辑每条评论的固定代码
Rya*_*ndy 59
像上面提到的那样添加对Microsoft.VisualBasic.dll的引用.然后获得总物理内存就像这样简单(是的,我测试过它):
static ulong GetTotalMemoryInBytes()
{
return new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory;
}
Run Code Online (Sandbox Code Playgroud)
小智 29
如果您碰巧使用Mono,那么您可能有兴趣知道Mono 2.8(将于今年晚些时候发布)将有一个性能计数器,用于报告Mono运行的所有平台(包括Windows)上的物理内存大小.您可以使用以下代码片段检索计数器的值:
using System;
using System.Diagnostics;
class app
{
static void Main ()
{
var pc = new PerformanceCounter ("Mono Memory", "Total Physical Memory");
Console.WriteLine ("Physical RAM (bytes): {0}", pc.RawValue);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您对提供性能计数器的C代码感兴趣,可以在此处找到它.
sst*_*tan 26
这里的所有答案,包括已接受的答案,都将为您提供可用的RAM总量.这可能是OP想要的.
但是,如果您对获取已安装的 RAM 数量感兴趣,那么您将需要调用GetPhysicallyInstalledSystemMemory函数.
从链接中,在备注部分:
该GetPhysicallyInstalledSystemMemory功能从计算机的SMBIOS固件表中检索物理安装的RAM量.这可能与GlobalMemoryStatusEx函数报告的数量不同,后者将MEMORYSTATUSEX结构的ullTotalPhys成员设置为可供操作系统使用的物理内存量.操作系统可用的内存量可能小于计算机中物理安装的内存量,因为BIOS和某些驱动程序可能会将内存保留为内存映射设备的I/O区域,从而使操作系统无法访问内存和应用程序.
示例代码:
[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.");
}
Run Code Online (Sandbox Code Playgroud)
BRA*_*mel 15
对于正在使用的用户.net Core 3.0
,无需使用PInvoke
平台即可获得可用的物理内存。本GC
类增加了新的方法GC.GetGCMemoryInfo
,它返回一个GCMemoryInfo Struct
带有TotalAvailableMemoryBytes
作为属性。此属性返回垃圾收集器的总可用内存。(与 MEMORYSTATUSEX 相同的值)
var gcMemoryInfo = GC.GetGCMemoryInfo();
installedMemory = gcMemoryInfo.TotalAvailableMemoryBytes;
// it will give the size of memory in MB
var physicalMemory = (double) installedMemory / 1048576.0;
Run Code Online (Sandbox Code Playgroud)
zge*_*erd 11
另一种方法是使用.NET System.Management查询工具:
string Query = "SELECT Capacity FROM Win32_PhysicalMemory";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
UInt64 Capacity = 0;
foreach (ManagementObject WniPART in searcher.Get())
{
Capacity += Convert.ToUInt64(WniPART.Properties["Capacity"].Value);
}
return Capacity;
Run Code Online (Sandbox Code Playgroud)
小智 6
您只需使用此代码即可获取这些信息,只需添加引用即可
using Microsoft.VisualBasic.Devices;
Run Code Online (Sandbox Code Playgroud)
并简单地使用以下代码
private void button1_Click(object sender, EventArgs e)
{
getAvailableRAM();
}
public void getAvailableRAM()
{
ComputerInfo CI = new ComputerInfo();
ulong mem = ulong.Parse(CI.TotalPhysicalMemory.ToString());
richTextBox1.Text = (mem / (1024*1024) + " MB").ToString();
}
Run Code Online (Sandbox Code Playgroud)
小智 5
你可以使用 WMI。发现一个片段。
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
strMemory = objComputer.TotalPhysicalMemory
Next
Run Code Online (Sandbox Code Playgroud)
小智 5
// use `/ 1048576` to get ram in MB
// and `/ (1048576 * 1024)` or `/ 1048576 / 1024` to get ram in GB
private static String getRAMsize()
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject item in moc)
{
return Convert.ToString(Math.Round(Convert.ToDouble(item.Properties["TotalPhysicalMemory"].Value) / 1048576, 0)) + " MB";
}
return "RAMsize";
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
120243 次 |
最近记录: |