Ejemplo n.º 1
0
        /// <inheritdoc />
        public EmptyWorkingSetResult TryEmptyWorkingSet(bool isSuspend)
        {
            (JobObject jobObject, uint[] childProcesses) = GetJobObjectWithChildProcessIds();

            if (jobObject == null)
            {
                return(EmptyWorkingSetResult.None);
            }

            EmptyWorkingSetResult result = EmptyWorkingSetResult.Success;

            VisitJobObjectProcesses(jobObject, childProcesses, (processHandle, pid) =>
            {
                if (isSuspend)
                {
                    bool isSuspendFailed = false;
                    try
                    {
                        isSuspendFailed = !Interop.Windows.Process.Suspend(System.Diagnostics.Process.GetProcessById((int)pid));
                    }
#pragma warning disable ERP022
                    catch (Exception)
                    {
                        isSuspendFailed = true;
                    }
#pragma warning restore ERP022

                    if (isSuspendFailed)
                    {
                        // If suspending the process fails, no need to continue going through the other processes.
                        result |= EmptyWorkingSetResult.SuspendFailed;
                    }
                }

                if (!Interop.Windows.Memory.EmptyWorkingSet(processHandle.DangerousGetHandle()))
                {
                    result |= EmptyWorkingSetResult.EmptyWorkingSetFailed;
                }

                if (EngineEnvironmentSettings.SetMaxWorkingSetToMin)
                {
                    if (!Interop.Windows.Memory.SetProcessWorkingSetSizeEx(
                            processHandle.DangerousGetHandle(),
                            DefaultMin, // the default on systems with 4k pages
                            DefaultMin,
                            Interop.Windows.Memory.WorkingSetSizeFlags.MaxEnable | Interop.Windows.Memory.WorkingSetSizeFlags.MinDisable))
                    {
                        result |= EmptyWorkingSetResult.SetMaxWorkingSetFailed;
                    }
                }
            });


            return(result);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public EmptyWorkingSetResult TryEmptyWorkingSet(bool isSuspend)
        {
            // Accessing jobObject needs to be protected by m_queryJobDataLock.
            // After we collected the child process ids, we might dispose the job object and the child process might be invalid.
            // Acquiring the reader lock will prevent the job object from disposing.
            using (m_queryJobDataLock.AcquireReadLock())
            {
                (JobObject jobObject, uint[] childProcesses) = GetJobObjectWithChildProcessIds();

                if (jobObject == null)
                {
                    return(EmptyWorkingSetResult.None);
                }

                EmptyWorkingSetResult result = EmptyWorkingSetResult.Success;

                VisitJobObjectProcesses(jobObject, childProcesses, (processHandle, pid) =>
                {
                    if (isSuspend)
                    {
                        bool isSuspendFailed = false;
                        try
                        {
                            isSuspendFailed = !Interop.Windows.Process.Suspend(System.Diagnostics.Process.GetProcessById((int)pid));
                        }
#pragma warning disable ERP022
                        catch (Exception)
                        {
                            isSuspendFailed = true;
                        }
#pragma warning restore ERP022

                        if (isSuspendFailed)
                        {
                            // If suspending the process fails, no need to continue going through the other processes.
                            result |= EmptyWorkingSetResult.SuspendFailed;
                        }
                    }

                    if (!Interop.Windows.Memory.EmptyWorkingSet(processHandle.DangerousGetHandle()))
                    {
                        result |= EmptyWorkingSetResult.EmptyWorkingSetFailed;
                    }

                    if (EngineEnvironmentSettings.SetMaxWorkingSetToMin)
                    {
                        if (!Interop.Windows.Memory.SetProcessWorkingSetSizeEx(
                                processHandle.DangerousGetHandle(),
                                s_defaultMin, // the default on systems with 4k pages
                                s_defaultMin,
                                Interop.Windows.Memory.WorkingSetSizeFlags.MaxEnable | Interop.Windows.Memory.WorkingSetSizeFlags.MinDisable))
                        {
                            result |= EmptyWorkingSetResult.SetMaxWorkingSetFailed;
                        }
                    }
                });


                return(result);
            }
        }