開發完成之后發現自己寫的程序內存占用太高,找到如下解決方案
使用了一個timer每2s調用一次ClearMemory()
#region 內存回收 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")] public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); /// <summary> /// 釋放內存 /// </summary> public static void ClearMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } } private void timer2_Tick(object sender, EventArgs e) { ClearMemory(); } #endregion