/// <summary>
        /// Estimates memory consumption
        /// </summary>
        /// <exception cref="InvalidOperationException">Process has exited</exception>
        /// <returns></returns>
        public ProcessMemoryInfo GetMemoryInfo(int pid)
        {
            var handle = IntPtr.Zero;

            try
            {
                handle = ProcessUtility.OpenLimitedQueryInformationProcessHandle(pid);
                EnsureProcessIsRunning(handle, pid);

                if (!PsApi.GetProcessMemoryInfo(handle, out var counters))
                {
                    Win32ExceptionUtility.Throw();
                }

                return(new ProcessMemoryInfo
                {
                    // use PrivateUsage instead of PagefileUsage because PagefileUsage is always zero on Windows 7, Windows Server 2008 R2 and earlier systems
                    PrivateBytes = (long)counters.PrivateUsage,
                    WorkingSetBytes = (long)counters.WorkingSetSize
                });
            }
            finally
            {
                Kernel32.CloseHandle(handle);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates ProcessMemoryMeter
 /// </summary>
 /// <param name="processId"></param>
 public ProcessMemoryMeter(int processId)
 {
     pid    = processId;
     handle = ProcessUtility.OpenLimitedQueryInformationProcessHandle(processId);
 }
 /// <summary>
 /// Creates <see cref="ProcessCpuUsageMeter"/> for specified process.
 /// </summary>
 public ProcessCpuUsageMeter(int pid)
 {
     handle        = ProcessUtility.OpenLimitedQueryInformationProcessHandle(pid);
     cpuUsageMeter = new CpuUsageMeter(() => CpuUsageNativeApi.GetProcessTime(handle));
 }