Created
October 16, 2018 22:23
-
-
Save mjs3339/5efdc2977a2e2b9be22e127000dc0418 to your computer and use it in GitHub Desktop.
C# MemoryInfo Get Windows Memory Information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MemoryInfo | |
{ | |
[Flags] | |
public enum AllocationType : uint | |
{ | |
COMMIT = 0x1000, | |
RESERVE = 0x2000, | |
RESET = 0x80000, | |
LARGE_PAGES = 0x20000000, | |
PHYSICAL = 0x400000, | |
TOP_DOWN = 0x100000, | |
WRITE_WATCH = 0x200000 | |
} | |
[Flags] | |
public enum MemoryProtection : uint | |
{ | |
EXECUTE = 0x10, | |
EXECUTE_READ = 0x20, | |
EXECUTE_READWRITE = 0x40, | |
EXECUTE_WRITECOPY = 0x80, | |
NOACCESS = 0x01, | |
READONLY = 0x02, | |
READWRITE = 0x04, | |
WRITECOPY = 0x08, | |
GUARD_Modifierflag = 0x100, | |
NOCACHE_Modifierflag = 0x200, | |
WRITECOMBINE_Modifierflag = 0x400 | |
} | |
[DllImport("psapi.dll", SetLastError = true)] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size); | |
public static RAM GetValues() | |
{ | |
var ram = new RAM(); | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
{ | |
ram.CommitLimit = Convert.ToInt64(pi.CommitLimit.ToInt64() * pi.PageSize.ToInt64()); | |
ram.CommitPeak = Convert.ToInt64(pi.CommitPeak.ToInt64() * pi.PageSize.ToInt64()); | |
ram.CommitTotal = Convert.ToInt64(pi.CommitTotal.ToInt64() * pi.PageSize.ToInt64()); | |
ram.KernelNonPaged = Convert.ToInt64(pi.KernelNonPaged.ToInt64() * pi.PageSize.ToInt64()); | |
ram.KernelPaged = Convert.ToInt64(pi.KernelPaged.ToInt64() * pi.PageSize.ToInt64()); | |
ram.KernelTotal = Convert.ToInt64(pi.KernelTotal.ToInt64() * pi.PageSize.ToInt64()); | |
ram.PageSize = Convert.ToInt64(pi.PageSize.ToInt64()); | |
ram.PhysicalAvailable = Convert.ToInt64(pi.PhysicalAvailable.ToInt64() * pi.PageSize.ToInt64()); | |
ram.PhysicalTotal = Convert.ToInt64(pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64()); | |
ram.ProcessCount = pi.ProcessCount; | |
ram.SystemCache = Convert.ToInt64(pi.SystemCache.ToInt64() * pi.PageSize.ToInt64()); | |
ram.ThreadCount = pi.ThreadCount; | |
ram.HandlesCount = pi.HandlesCount; | |
ram.PhysicalAvailableAsPercent = ram.PhysicalAvailable / (float) ram.PhysicalTotal * 100.0f; | |
ram.CommitLimitAsPercent = ram.CommitLimit / (float) ram.PhysicalTotal * 100.0f; | |
ram.CommitPeakAsPercent = ram.CommitPeak / (float) ram.PhysicalTotal * 100.0f; | |
ram.CommitTotalAsPercent = ram.CommitTotal / (float) ram.PhysicalTotal * 100.0f; | |
} | |
return ram; | |
} | |
public static int GetThreadCount() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return pi.ThreadCount; | |
return-1; | |
} | |
public static int GetProcessCount() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return pi.ProcessCount; | |
return-1; | |
} | |
public static int GetHandlesCount() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return pi.HandlesCount; | |
return-1; | |
} | |
public static long GetPageSize() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.PageSize.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetKernelNonPaged() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.KernelNonPaged.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetKernelPaged() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.KernelPaged.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetKernelTotal() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.KernelTotal.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetSystemCache() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.SystemCache.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetCommitLimit() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.CommitLimit.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetAvailableMemory() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.PhysicalAvailable.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetTotalMemory() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetCommitCharge() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.CommitTotal.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
public static long GetPeakCommitCharge() | |
{ | |
var pi = new PerformanceInformation(); | |
if(GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) | |
return Convert.ToInt64(pi.CommitPeak.ToInt64() * pi.PageSize.ToInt64()); | |
return-1; | |
} | |
[DllImport("ntdll.dll", CharSet = CharSet.Auto)] | |
public static extern uint NtQuerySystemInformation(int SystemInformationClass, IntPtr SystemInformation, int SystemInformationLength, ref int returnLength); | |
public static long GetCommited() | |
{ | |
var retLength = 512; | |
var Handle = new SYSTEM_PERFORMANCE_INFORMATION(); | |
var lpBuffer = Marshal.AllocHGlobal(retLength); | |
NtQuerySystemInformation(2, lpBuffer, retLength, ref retLength); | |
Marshal.FreeHGlobal(lpBuffer); | |
lpBuffer = Marshal.AllocHGlobal(retLength); | |
NtQuerySystemInformation(2, lpBuffer, retLength, ref retLength); | |
var intPtr = new IntPtr(lpBuffer.ToInt64()); | |
Handle = (SYSTEM_PERFORMANCE_INFORMATION) Marshal.PtrToStructure(intPtr, Handle.GetType()); | |
return(long) Handle.CommittedPages * 4096; | |
} | |
public static long GetCommitChargeEx() | |
{ | |
var p = new PerformanceCounter("Memory", "Committed Bytes"); | |
return p.RawValue; | |
} | |
public static Task<ArrayList> QueryWMIObjectAsync(string queryObject) | |
{ | |
return Task.Factory.StartNew(() => | |
{ | |
var hd = new ArrayList(); | |
try | |
{ | |
var Searcher = new ManagementObjectSearcher("select * from " + queryObject); | |
foreach(var wmi_HD in Searcher.Get()) | |
{ | |
var searcherProperties = wmi_HD.Properties; | |
foreach(var sp in searcherProperties) | |
hd.Add(sp); | |
} | |
} | |
catch | |
{ | |
} | |
return hd; | |
}); | |
} | |
public static ArrayList QueryWMIObject(string queryObject) | |
{ | |
var hd = new ArrayList(); | |
try | |
{ | |
var Searcher = new ManagementObjectSearcher("select * from " + queryObject); | |
foreach(var wmi_HD in Searcher.Get()) | |
{ | |
var searcherProperties = wmi_HD.Properties; | |
foreach(var sp in searcherProperties) | |
hd.Add(sp); | |
} | |
} | |
catch | |
{ | |
} | |
return hd; | |
} | |
public static ulong GetTotalMemoryEx() | |
{ | |
var CI = new ComputerInfo(); | |
return CI.TotalPhysicalMemory; | |
} | |
public static ulong GetFreeRAM() | |
{ | |
var CI = new ComputerInfo(); | |
return CI.AvailablePhysicalMemory; | |
} | |
[DllImport("kernel32.dll", SetLastError = true)] | |
private static extern IntPtr VirtualAlloc(IntPtr lpAddress, ulong dwSize, AllocationType flAllocationType, MemoryProtection flProtect); | |
[DllImport("kernel32")] | |
private static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType); | |
public static IntPtr VAMemory(ulong Size) | |
{ | |
var p = VirtualAlloc(IntPtr.Zero, Size, AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.READWRITE); | |
var ec = Marshal.GetLastWin32Error(); | |
var ex = new Win32Exception(); | |
return p; | |
} | |
public static void VAFree(IntPtr p) | |
{ | |
VirtualFree(p, 0, 0x8000); | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct PerformanceInformation | |
{ | |
public int Size; | |
public IntPtr CommitTotal; | |
public IntPtr CommitLimit; | |
public IntPtr CommitPeak; | |
public IntPtr PhysicalTotal; | |
public IntPtr PhysicalAvailable; | |
public IntPtr SystemCache; | |
public IntPtr KernelTotal; | |
public IntPtr KernelPaged; | |
public IntPtr KernelNonPaged; | |
public IntPtr PageSize; | |
public int HandlesCount; | |
public int ProcessCount; | |
public int ThreadCount; | |
} | |
public struct RAM | |
{ | |
public long CommitLimit; | |
public float CommitLimitAsPercent; | |
public long CommitPeak; | |
public float CommitPeakAsPercent; | |
public long CommitTotal; | |
public float CommitTotalAsPercent; | |
public int HandlesCount; | |
public long KernelNonPaged; | |
public long KernelPaged; | |
public long KernelTotal; | |
public long PageSize; | |
public long PhysicalAvailable; | |
public float PhysicalAvailableAsPercent; | |
public long PhysicalTotal; | |
public int ProcessCount; | |
public long SystemCache; | |
public int ThreadCount; | |
} | |
public struct SYSTEM_PERFORMANCE_INFORMATION | |
{ | |
public int AvailablePages; | |
public int CacheRead; | |
public int CacheReadIos; | |
public int CacheTransitionFaults; | |
public int CommitLimit; | |
public int CommittedPages; | |
public int ContextSwitches; | |
public int CopyOnWriteFaults; | |
public int CopyReadNoWait; | |
public int CopyReadNoWaitMiss; | |
public int CopyReadWait; | |
public int CopyReadWaitMiss; | |
public int DataFlushes; | |
public int DataPages; | |
public int DemandZeroFaults; | |
public int FastMdlReadNotPossible; | |
public int FastMdlReadNoWait; | |
public int FastMdlReadResourceMiss; | |
public int FastMdlReadWait; | |
public int FastReadNotPossible; | |
public int FastReadNoWait; | |
public int FastReadResourceMiss; | |
public int FastReadWait; | |
public int FirstLevelTbFills; | |
public int FreeSystemPtes; | |
public long IdleTime; | |
public int IoOtherOperationCount; | |
public long IoOtherTransferCount; | |
public int IoReadOperationCount; | |
public long IoReadTransferCount; | |
public int IoWriteOperationCount; | |
public long IoWriteTransferCount; | |
public int LazyWriteIos; | |
public int LazyWritePages; | |
public int MapDataNoWait; | |
public int MapDataNoWaitMiss; | |
public int MapDataWait; | |
public int MapDataWaitMiss; | |
public int MappedFilePagesWritten; | |
public int MappedFilePageWriteIos; | |
public int MdlReadNoWait; | |
public int MdlReadNoWaitMiss; | |
public int MdlReadWait; | |
public int MdlReadWaitMiss; | |
public int NonPagedPoolAllocs; | |
public int NonPagedPoolFrees; | |
public int NonPagedPoolUsage; | |
public int PagedPoolAllocs; | |
public int PagedPoolFrees; | |
public int PagedPoolPages; | |
public int PagedPoolUsage; | |
public int PageFaults; | |
public int PagefilePagesWriteIos; | |
public int PagefilePagesWritten; | |
public int PagesRead; | |
public int PagesReadIos; | |
public int PeakCommitment; | |
public int PinMappedDataCount; | |
public int PinReadNoWait; | |
public int PinReadNoWaitMiss; | |
public int PinReadWait; | |
public int PinReadWaitMiss; | |
public int ReadAheadIos; | |
public int Reserved3; | |
public int SecondLevelTbFills; | |
public int SmallNonPagedPoolLookasideListAllocateHits; | |
public int SmallPagedPoolLookasideAllocateHits; | |
public int SystemCachePages; | |
public int SystemCalls; | |
public int SystemCodePages; | |
public int SystemDriverPages; | |
public int TotalSystemCodePages; | |
public int TotalSystemDriverPages; | |
public int TransitionFaults; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment