From 9aefc90460c20a1ae5cdb6632d586daef1fd4a6d Mon Sep 17 00:00:00 2001 From: RKrom Date: Mon, 16 Jun 2014 23:32:30 +0200 Subject: [PATCH] Usage of EmptyWorkingSet now via the configuration, so we don't use it for all people. --- Greenshot/Forms/MainForm.cs | 18 +++++------------- Greenshot/Helpers/CaptureHelper.cs | 4 ++++ GreenshotPlugin/UnmanagedHelpers/PsAPI.cs | 14 +++++++++++++- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index c2cf1f5e4..9db5260a6 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -408,7 +408,9 @@ namespace Greenshot { HandleDataTransport(dataTransport); } // Make Greenshot use less memory after startup - EmptyWorkingSet(); + if (_conf.MinimizeWorkingSetSize) { + PsAPI.EmptyWorkingSet(); + } } /// @@ -1253,8 +1255,7 @@ namespace Greenshot { if (path != null) { try { - using (Process process = Process.Start(path)) { - } + using (Process.Start(path)) {} } catch (Exception ex) { // Make sure we show what we tried to open in the exception ex.Data.Add("path", path); @@ -1379,14 +1380,6 @@ namespace Greenshot { } } - /// - /// Make the process use less memory by emptying the working set - /// - private void EmptyWorkingSet() { - using (Process currentProcess = Process.GetCurrentProcess()) { - PsAPI.EmptyWorkingSet(currentProcess.Handle); - } - } /// /// Do work in the background @@ -1395,8 +1388,7 @@ namespace Greenshot { /// private void BackgroundWorkerTimerTick(object sender, EventArgs e) { if (_conf.MinimizeWorkingSetSize) { - LOG.Info("Calling EmptyWorkingSet"); - EmptyWorkingSet(); + PsAPI.EmptyWorkingSet(); } if (UpdateHelper.IsUpdateCheckNeeded()) { LOG.Debug("BackgroundWorkerTimerTick checking for update"); diff --git a/Greenshot/Helpers/CaptureHelper.cs b/Greenshot/Helpers/CaptureHelper.cs index 72f2e86c7..e627b85af 100644 --- a/Greenshot/Helpers/CaptureHelper.cs +++ b/Greenshot/Helpers/CaptureHelper.cs @@ -77,6 +77,10 @@ namespace Greenshot.Helpers { _windows = null; _selectedCaptureWindow = null; _capture = null; + // Empty working set after capturing + if (conf.MinimizeWorkingSetSize) { + PsAPI.EmptyWorkingSet(); + } } public static void CaptureClipboard() { using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.Clipboard)) { diff --git a/GreenshotPlugin/UnmanagedHelpers/PsAPI.cs b/GreenshotPlugin/UnmanagedHelpers/PsAPI.cs index 64e5db998..9ffc8db3f 100644 --- a/GreenshotPlugin/UnmanagedHelpers/PsAPI.cs +++ b/GreenshotPlugin/UnmanagedHelpers/PsAPI.cs @@ -19,20 +19,32 @@ * along with this program. If not, see . */ using System; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; +using log4net; namespace GreenshotPlugin.UnmanagedHelpers { /// /// Description of PsAPI. /// public class PsAPI { + private static readonly ILog LOG = LogManager.GetLogger(typeof(PsAPI)); [DllImport("psapi", SetLastError = true, CharSet=CharSet.Unicode)] public static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, StringBuilder lpFilename, uint nSize); [DllImport("psapi", SetLastError = true, CharSet = CharSet.Unicode)] public static extern uint GetProcessImageFileName(IntPtr hProcess, StringBuilder lpImageFileName, uint nSize); [DllImport("psapi")] - public static extern int EmptyWorkingSet(IntPtr hwProc); + private static extern int EmptyWorkingSet(IntPtr hwProc); + /// + /// Make the process use less memory by emptying the working set + /// + public static void EmptyWorkingSet() { + LOG.Info("Calling EmptyWorkingSet"); + using (Process currentProcess = Process.GetCurrentProcess()) { + EmptyWorkingSet(currentProcess.Handle); + } + } } }