我正在努力计算记忆力.我使用以下代码计算了Available,InUse,Free和Cached
ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
//total amount of free physical memory in bytes
var Available = new ComputerInfo().AvailablePhysicalMemory;
//total amount of physical memory in bytes
var Total = new ComputerInfo().TotalPhysicalMemory;
var PhysicalMemoryInUse = Total - Available;
Object Free = new object();
foreach (var result in results)
{
//Free amount
Free = result["FreePhysicalMemory"];
}
var Cached = Total - PhysicalMemoryInUse - UInt64.Parse(Free.ToString());
Run Code Online (Sandbox Code Playgroud)
如何计算Windows中资源监视器中显示的待机,硬件保留和修改内存?
保留的硬件是物理安装的内存量与OS报告的物理内存总量之间的差异.
其他信息可以通过性能计数器检索.我在下面有一个示例类,但是知道这不是一个强大的实现.您需要添加适当的错误处理和资源清理.但是,缓存此类的实例并Refresh()在需要时通过更新值应该可以很好地执行.
public sealed class MemoryInfo : IDisposable
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetPhysicallyInstalledSystemMemory(out ulong memoryInKilobytes);
private readonly PerformanceCounter availableCounter;
private readonly PerformanceCounter modifiedCounter;
private readonly PerformanceCounter freeCounter;
private readonly PerformanceCounter standbyCoreCounter;
private readonly PerformanceCounter standbyNormalCounter;
private readonly PerformanceCounter standbyReserveCounter;
private ulong osTotalMemory;
public ulong ModifiedBytes { get; private set; }
public ulong InUseBytes { get; private set; }
public ulong StandbyBytes { get; private set; }
public ulong FreeBytes { get; private set; }
public ulong HardwareReserved { get; }
public MemoryInfo()
{
var computerInfo = new ComputerInfo();
osTotalMemory = computerInfo.TotalPhysicalMemory;
ulong installedPhysicalMemInKb;
GetPhysicallyInstalledSystemMemory(out installedPhysicalMemInKb);
this.HardwareReserved = installedPhysicalMemInKb * 1024 - osTotalMemory;
modifiedCounter = new PerformanceCounter("Memory", "Modified Page List Bytes");
standbyCoreCounter = new PerformanceCounter("Memory", "Standby Cache Core Bytes");
standbyNormalCounter = new PerformanceCounter("Memory", "Standby Cache Normal Priority Bytes");
standbyReserveCounter = new PerformanceCounter("Memory", "Standby Cache Reserve Bytes");
freeCounter = new PerformanceCounter("Memory", "Free & Zero Page List Bytes");
availableCounter = new PerformanceCounter("Memory", "Available Bytes");
Refresh();
}
public void Refresh()
{
ModifiedBytes = (ulong)modifiedCounter.NextSample().RawValue;
StandbyBytes = (ulong)standbyCoreCounter.NextSample().RawValue +
(ulong)standbyNormalCounter.NextSample().RawValue +
(ulong)standbyReserveCounter.NextSample().RawValue;
FreeBytes = (ulong)freeCounter.NextSample().RawValue;
InUseBytes = osTotalMemory - (ulong) availableCounter.NextSample().RawValue;
}
public void Dispose()
{
modifiedCounter.Dispose();
standbyCoreCounter.Dispose();
standbyNormalCounter.Dispose();
standbyReserveCounter.Dispose();
freeCounter.Dispose();
availableCounter.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
这样做有一些缺点,比如perf计数器没有组合在一起,所以你不会在特定的时间点获得系统内存的"真实"快照.您可以使用PInvoke直接使用Pdh*win32 api函数来改进它.
您也可以将其更改为使用WMI(数据输入Win32_PerfRawData_PerfOS_Memory),但我不知道它将如何执行.