/// <nodoc /> public static PERFORMANCE_INFORMATION CreatePerfInfo() { PERFORMANCE_INFORMATION pi = new PERFORMANCE_INFORMATION(); pi.cb = (uint)Marshal.SizeOf(typeof(PERFORMANCE_INFORMATION)); return(pi); }
internal string Memory() { string memory = null; while (true) { PERFORMANCE_INFORMATION pi = new PERFORMANCE_INFORMATION(); pi.Initialize(); SafeNativeMethods.GetPerformanceInfo(out pi, pi.cb); SafeNativeMethods.GetPhysicallyInstalledSystemMemory(out ulong installedMemory); MEMORYSTATUSEX globalMemoryStatus = new MEMORYSTATUSEX(); globalMemoryStatus.Initialize(); SafeNativeMethods.GlobalMemoryStatusEx(ref globalMemoryStatus); if (installedMemory >= 1024) { memory = $"{installedMemory / 1024} MB RAM"; break; } else { memory = $"{installedMemory} RAM"; break; } } return(memory); }
public void GetMemory() { try { PERFORMANCE_INFORMATION pi = new PERFORMANCE_INFORMATION(); pi.Initialize(); GetPerformanceInfo(out pi, pi.cb); ulong modified = (ulong)_modifiedMemory.RawValue; ulong inuse = pi.Total - pi.Available - modified; if (!string.IsNullOrEmpty(_modifiedMemory.InstanceName)) { inuse = modified; } ulong InstalledSystemMemory = 0; GetPhysicallyInstalledSystemMemory(out InstalledSystemMemory); _InstalledMemory = (float)InstalledSystemMemory / 1024.0f / 1024.0f; _Usage = (float)pi.Total / 1024.0f / 1024.0f / 1024.0f; _UseMemory = (float)inuse / 1024.0f / 1024.0f / 1024.0f; } catch (Exception ex) { Console.WriteLine(string.Format("[{0}] - {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message.Replace("'", ""))); } }
public static PERFORMANCE_INFORMATION GetPerfInfo() { PERFORMANCE_INFORMATION _memData = new PERFORMANCE_INFORMATION(); _memData.cb = (uint)Marshal.SizeOf(_memData); if (!_GetPerformanceInfo(ref _memData, _memData.cb)) { string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; if (Marshal.GetLastWin32Error() != 0) { Console.WriteLine("MARSHAL ERROR: " + errorMessage); } } return(_memData); }
private void CollectOnce() { lock (m_collectLock) { // This must be reacquired for every collection and may not be cached because some of the fields like memory // usage are only set in the Process() constructor Process currentProcess = Process.GetCurrentProcess(); // Compute the performance data double?machineCpu = 0.0; double?processCpu = GetProcessCpu(currentProcess); double processThreads = currentProcess.Threads.Count; double processPrivateBytes = currentProcess.PrivateMemorySize64; double processWorkingSetBytes = currentProcess.WorkingSet64; double processHeldBytes = m_collectHeldBytesFromGC ? GC.GetTotalMemory(forceFullCollection: true) : 0; double? machineAvailablePhysicalBytes = null; double? machineTotalPhysicalBytes = null; double? commitTotalBytes = null; double? commitLimitBytes = null; DISK_PERFORMANCE[] diskStats = null; if (!OperatingSystemHelper.IsUnixOS) { machineCpu = GetMachineCpu(); MEMORYSTATUSEX memoryStatusEx = new MEMORYSTATUSEX(); if (GlobalMemoryStatusEx(memoryStatusEx)) { machineAvailablePhysicalBytes = memoryStatusEx.ullAvailPhys; machineTotalPhysicalBytes = memoryStatusEx.ullTotalPhys; } PERFORMANCE_INFORMATION performanceInfo = PERFORMANCE_INFORMATION.CreatePerfInfo(); if (GetPerformanceInfo(out performanceInfo, performanceInfo.cb)) { commitTotalBytes = performanceInfo.CommitTotal.ToInt64() * performanceInfo.PageSize.ToInt64(); commitLimitBytes = performanceInfo.CommitLimit.ToInt64() * performanceInfo.PageSize.ToInt64(); } diskStats = GetDiskCounters(); } else { machineCpu = GetMachineCpuMacOS(); ulong totalPhysicalBytes = OperatingSystemHelper.GetPhysicalMemorySize().Bytes; machineTotalPhysicalBytes = totalPhysicalBytes; machineAvailablePhysicalBytes = GetAvailablePhysicalBytesMacOS(totalPhysicalBytes); } // stop network monitor measurement and gather data m_networkMonitor?.StopMeasurement(); DateTime temp = DateTime.UtcNow; TimeSpan duration = temp - m_networkTimeLastCollectedAt; m_networkTimeLastCollectedAt = temp; double?machineKbitsPerSecSent = null; double?machineKbitsPerSecReceived = null; if (m_networkMonitor != null) { machineKbitsPerSecSent = Math.Round(1000 * BytesToKbits(m_networkMonitor.TotalSentBytes) / Math.Max(duration.TotalMilliseconds, 1.0), 3); machineKbitsPerSecReceived = Math.Round(1000 * BytesToKbits(m_networkMonitor.TotalReceivedBytes) / Math.Max(duration.TotalMilliseconds, 1.0), 3); } // Update the aggregators lock (m_aggregators) { foreach (var aggregator in m_aggregators) { aggregator.RegisterSample( processCpu: processCpu, processPrivateBytes: processPrivateBytes, processWorkingSetBytes: processWorkingSetBytes, threads: processThreads, machineCpu: machineCpu, machineTotalPhysicalBytes: machineTotalPhysicalBytes, machineAvailablePhysicalBytes: machineAvailablePhysicalBytes, commitTotalBytes: commitTotalBytes, commitLimitBytes: commitLimitBytes, machineBandwidth: m_networkMonitor?.Bandwidth, machineKbitsPerSecSent: machineKbitsPerSecSent, machineKbitsPerSecReceived: machineKbitsPerSecReceived, diskPerformance: diskStats, gcHeldBytes: processHeldBytes); } } // restart network monitor to start new measurement m_networkMonitor?.StartMeasurement(); } }
static void Main(string[] args) { freeMemory = new PerformanceCounter("Memory", "Free & Zero Page List Bytes", true); modifiedMemory = new PerformanceCounter("Memory", "Modified Page List Bytes", true); while (true) { PERFORMANCE_INFORMATION pi = new PERFORMANCE_INFORMATION(); pi.Initialize(); SafeNativeMethods.GetPerformanceInfo(out pi, pi.cb); Console.WriteLine("[Resource Monitor]"); SafeNativeMethods.GetPhysicallyInstalledSystemMemory(out ulong installedMemory); double reserved = (installedMemory - (pi.Total / 1024.0)); ulong modified = (ulong)modifiedMemory.RawValue; ulong inuse = pi.Total - pi.Available - modified; long reservedMB = (long)Math.Round(reserved / 1024.0); Console.WriteLine($"Hardware Reserved: {reservedMB} MB"); Console.WriteLine($"In Use: {inuse / 1024 / 1024} MB"); Console.WriteLine($"Modified: {modified / 1024 / 1024} MB"); ulong free = (ulong)freeMemory.RawValue; ulong standby = pi.Available - free; Console.WriteLine($"Standby: {standby / 1024 / 1024} MB"); Console.WriteLine($"Free: {free / 1024 / 1024} MB"); Console.WriteLine(); Console.WriteLine($"Available: {pi.Available.MB()} MB"); Console.WriteLine($"Cached: {(standby + modified).MB()} MB"); Console.WriteLine($"Total: {pi.Total.MB()} MB"); Console.WriteLine($"Installed: {installedMemory / 1024} MB"); //IntPtr queryResult = SafeNativeMethods.NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemFullMemoryInformation, 0); //if (queryResult == IntPtr.Zero) //{ // Console.WriteLine(queryResult.ToInt64()); //} MEMORYSTATUSEX globalMemoryStatus = new MEMORYSTATUSEX(); globalMemoryStatus.Initialize(); SafeNativeMethods.GlobalMemoryStatusEx(ref globalMemoryStatus); Console.WriteLine(); Console.WriteLine("[Task Manager]"); Console.WriteLine($"Memory: {installedMemory / 1024.0 / 1024.0} GB"); Console.WriteLine($"Memory usage: {pi.Total / 1024.0 / 1024.0 / 1024.0:#.0} GB"); Console.WriteLine(); Console.WriteLine($"In use: {inuse / 1024.0 / 1024.0 / 1024.0:#.0} GB"); Console.WriteLine($"Available: {pi.Available / 1024.0 / 1024.0 / 1024.0:#.0} GB"); Console.WriteLine($"Committed: {pi.Commit / 1024.0 / 1024.0 / 1024.0:#.0} / {globalMemoryStatus.ullTotalPageFile / 1024.0 / 1024.0 / 1024.0:#.0} GB"); Console.WriteLine($"Cached: {(standby + modified) / 1024.0 / 1024.0 / 1024.0:#.0} GB"); Console.WriteLine($"Paged pool: {pi.KernelPage / 1024.0 / 1024.0 / 1024.0:#.0} GB"); Console.WriteLine($"Non-paged pool: {pi.KernelNonPage / 1024.0 / 1024.0:#} MB"); Console.WriteLine(); Thread.Sleep(1000); } }
public static extern bool GetPerformanceInfo(out PERFORMANCE_INFORMATION pPerformanceInformation, uint cb);
public static extern bool GetPerformanceInfo(ref PERFORMANCE_INFORMATION PerformanceInformation, int Size);
public static extern bool GetPerformanceInfo(ref PERFORMANCE_INFORMATION PerformanceInformation, int Size);
public extern static Boolean GetPerformanceInfo( [Out] out PERFORMANCE_INFORMATION pPerformanceInformation, [In] UInt32 cb );
private static extern bool GetPerformanceInfo( out PERFORMANCE_INFORMATION pPerformanceInformation, [In] int cb );
public static unsafe bool GetPerformanceInfo(out PERFORMANCE_INFORMATION performanceInfo) => GetPerformanceInfo(out performanceInfo, sizeof(PERFORMANCE_INFORMATION));
private static extern bool _GetPerformanceInfo(ref PERFORMANCE_INFORMATION pPerformanceInformation, uint cb);