Пример #1
0
        /// <summary>
        /// Returns the memory counters of a specific process
        /// </summary>
        /// <param name="handle">When calling from Windows the SafeProcessHandle is required</param>
        /// <param name="pid">On non-windows systems a process id has to be provided</param>
        public static ProcessMemoryCountersSnapshot?GetMemoryCountersSnapshot(IntPtr handle, int pid)
        {
            switch (s_currentOS)
            {
            case OperatingSystem.Win:
                var counters = Windows.Memory.GetMemoryUsageCounters(handle);
                if (counters != null)
                {
                    return(ProcessMemoryCountersSnapshot.CreateFromBytes(
                               counters.PeakWorkingSetSize,
                               counters.WorkingSetSize,
                               (counters.WorkingSetSize + counters.PeakWorkingSetSize) / 2,
                               counters.PeakPagefileUsage,
                               counters.PagefileUsage));
                }

                return(null);

            default:
                ulong peakMemoryUsage = 0;
                if (Unix.Memory.GetPeakWorkingSetSize(pid, ref peakMemoryUsage) == MACOS_INTEROP_SUCCESS)
                {
                    return(ProcessMemoryCountersSnapshot.CreateFromBytes(
                               peakWorkingSet: peakMemoryUsage,
                               lastWorkingSet: peakMemoryUsage,
                               averageWorkingSet: peakMemoryUsage,
                               peakCommitSize: 0,
                               lastCommitSize: 0));
                }

                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the memory counters of a specific process only when running on Windows,
        /// otherwise counters for a whole processes tree rooted at the given process id.
        /// </summary>
        /// <param name="handle">When calling from Windows the SafeProcessHandle is required</param>
        /// <param name="pid">On non-windows systems a process id has to be provided</param>
        public static ProcessMemoryCountersSnapshot?GetMemoryCountersSnapshot(IntPtr handle, int pid)
        {
            switch (s_currentOS)
            {
            case OperatingSystem.Win:
            {
                var counters = Windows.Memory.GetMemoryUsageCounters(handle);
                if (counters != null)
                {
                    return(ProcessMemoryCountersSnapshot.CreateFromBytes(
                               counters.PeakWorkingSetSize,
                               counters.WorkingSetSize,
                               (counters.WorkingSetSize + counters.PeakWorkingSetSize) / 2,
                               counters.PeakPagefileUsage,
                               counters.PagefileUsage));
                }

                return(null);
            }

            default:
            {
                var usage = new ProcessResourceUsage();
                if (Unix.Process.GetProcessMemoryUsage(pid, ref usage, includeChildProcesses: true) == MACOS_INTEROP_SUCCESS)
                {
                    return(ProcessMemoryCountersSnapshot.CreateFromBytes(
                               usage.PeakWorkingSetSize,
                               usage.WorkingSetSize,
                               (usage.WorkingSetSize + usage.PeakWorkingSetSize) / 2,
                               peakCommitSize: 0,
                               lastCommitSize: 0));
                }

                return(null);
            }
            }
        }