/// <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);
            }
        }
Пример #2
0
        /// <summary>
        /// Estimates memory consumption
        /// </summary>
        /// <exception cref="InvalidOperationException">Process has exited</exception>
        /// <returns></returns>
        public ProcessMemoryInfo GetMemoryInfo()
        {
            ProcessUtility.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,
            });
        }