#regionMemory Recycling[DllImport ("Kernel32.dll", EntryPoint ="SetProcessWorkingSetSize")] Public Static extern intSetProcessWorkingSetSize (INTPTR process,intMinSize,intmaxSize); /// <summary> ///Freeing Memory/// </summary> Public Static voidclearmemory () {GC. Collect (); Gc. WaitForPendingFinalizers (); if(Environment.OSVersion.Platform = =platformid.win32nt) {setprocessworkingsetsize (System.Diagnostics.Process.GetCurrentProcess (). Handle,-1, -1); } } #endregion
A test was made using the SetProcessWorkingSetSize method in the system, and after calling the method, the memory consumed dropped from 80M to 2M. (TaskManager observed) according to the word of autumn one leaf, so adjust workingset, Will likely cause a missing pages break, severely impacting performance. But from the use of the situation, there is no such phenomenon found
PublicForm1 () {InitializeComponent (); //check once every 1 minutesSystem.Timers.Timer Timer =NewSystem.Timers.Timer ( -* +); Timer. Elapsed+=timer_elapsed; Timer. Start (); } Private voidTimer_elapsed (Objectsender, System.Timers.ElapsedEventArgs e) { //1 Compare time is valid hh:mmDateTime StartTime =E.signaltime; stringTime = configurationmanager.appsettings["Clearmemory_perdaytime"];//"05:00" stringDtnow = StartTime. ToString ("YYYY-MM-DD hh:mm"); DateTime Daydefine= DateTime.Parse (starttime. ToString ("YYYY-MM-DD") +" "+Time ); if(Daydefine.tostring ("YYYY-MM-DD hh:mm") ==Dtnow) { //2 Starting the garbage collectorclearmemory (); Logwarning (DateTime.Now.ToLongTimeString ()+": Free up memory!"); } }
Note that the reference namespace: using System.Runtime.InteropServices;
. NET cleanup memory, cleaning up memory usage methods