ハードウェアは、物理的にインストールされたメモリの量と、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();
}
}
はPERFカウンターが一緒にグループ化されていないように、このようにそれをやっての欠点がありますので、あなたは、特定の時点でシステムメモリの「本当の」スナップショットを得ることはありません。おそらく、PInvokeを使用してPdh*
win32 api関数を直接使用するように改善することができます。
また、WMI(データはWin32_PerfRawData_PerfOS_Memory
)を使用するように変更することもできますが、どのように実行するのか分かりません。
「スタンバイメモリ」と「変更されたメモリ」とは何ですか? – quetzalcoatl
@Scottあなたがリソースモニタから表示している値は、私が探している値です – nlstack01
これはあなたのために働くかどうか分かりませんか? https://msdn.microsoft.com/en-us/library/windows/desktop/dn958303(v=vs.85).aspx –