From 787c23ef7759e1f81626239715e200e0e3e4c934 Mon Sep 17 00:00:00 2001 From: liufei Date: Fri, 20 Aug 2021 16:49:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=9B=9E=E6=94=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Task/ToDoTask.cs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Task/ToDoTask.cs b/Task/ToDoTask.cs index 7aa2b46..0710b5a 100644 --- a/Task/ToDoTask.cs +++ b/Task/ToDoTask.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -24,9 +25,11 @@ namespace GeekDesk.Task public static void BackLogCheck() { - System.Timers.Timer timer = new System.Timers.Timer(); - timer.Enabled = true; - timer.Interval = 5000; + System.Timers.Timer timer = new System.Timers.Timer + { + Enabled = true, + Interval = 5000 + }; timer.Start(); timer.Elapsed += new System.Timers.ElapsedEventHandler(Check); } @@ -48,9 +51,27 @@ namespace GeekDesk.Task } } } + ClearMemory(); })); } + /// + /// 释放内存 + /// + public static void ClearMemory() + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + if (Environment.OSVersion.Platform == PlatformID.Win32NT) + { + SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); + } + } + + #region 内存回收 + [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")] + public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); + #endregion }