public static extern bool GetProcessMemoryInfo(IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, uint size);
public void Refresh(DateTime now) { if (_disposed) { return; } try { using (var processHandle = SafeProcessHandle.OpenProcess(Pid, ProcessAccessFlags.QueryInformation)) { unchecked { var counters = new PROCESS_MEMORY_COUNTERS(); NativeMethods.GetProcessMemoryInfo(processHandle.DangerousGetHandle(), out counters, (uint)Marshal.SizeOf(counters)); WorkingSet = counters.WorkingSetSize; PageFaultCount = counters.PageFaultCount; PageFileUsage = counters.PagefileUsage; PeakPageFileUsage = counters.PeakPagefileUsage; PeakWorkingSet = counters.PeakWorkingSetSize; PrivateBytes = counters.PrivateUsage; long createTime = 0; long exitTime = 0; long kernelTime = 0; long userTime = 0; NativeMethods.GetProcessTimes(processHandle.DangerousGetHandle(), ref createTime, ref exitTime, ref kernelTime, ref userTime); var cpuTime = new TimeSpan(kernelTime + userTime); CpuTime = string.Format(@"{0:00}:{1:00}:{2:00}", cpuTime.Hours, cpuTime.Minutes, cpuTime.Seconds); var latestProcTime = cpuTime - _previousTotalProcTime; var latestWallTime = (now - _lastCpuCheck).TotalMilliseconds; _previousTotalProcTime = cpuTime; _lastCpuCheck = now; if (latestWallTime > 0) { var totalCpu = 100 * latestProcTime.TotalMilliseconds / latestWallTime; var adjustedCpu = (int)Math.Round(totalCpu / Environment.ProcessorCount); PercentCpu = adjustedCpu.ToString(CultureInfo.InvariantCulture); } else { PercentCpu = "0"; } } } if (!_countersInitialized) { InitializePerformanceCounters(); } ManagedBytes = (_committedBytesCounter != null) ? ((long)_committedBytesCounter.NextValue()) : (0); } catch (Exception e) { Logger.Error("Error refreshing process performance info", e); } }