C # System. Timers. The use of the Timer and timed Automatic Cleaning of memory applications,
The project will be relatively difficult to use. Although there is an automatic GC cleaning mechanism, there are still some unsatisfactory aspects. Therefore, I tried to manually write a timer in the project Startup File to regularly clean up the memory and speed up the project operation.
Public class Program {[DllImport ("psapi. dll ")] static extern int EmptyWorkingSet (IntPtr hwProc); // clear memory-related static void Main () {// clear memory SetTimer () at startup ();} /// <summary> /// regularly clear memory /// </summary> private static void SetTimer () {System. timers. timer aTimer = new System. timers. timer (); // initialize the Timer aTimer. interval = 60000; // set the time to 1 minute aTimer. elapsed + = new System. timers. elapsedEventHandler (OnTimedEvent); aTime R. AutoReset = true; // The aTimer. Enabled = true is triggered every time the Elapsed event is specified; // indicates whether the Timer should trigger the Elapsed event.} // Processing Event private static void OnTimedEvent (Object source, ElapsedEventArgs e) triggered by the timer {// clear the memory GC. collect (); GC. waitForPendingFinalizers (); Process [] processes = Process. getProcesses (); foreach (Process process in processes) {// The following system processes do not have the permission to skip this step to prevent errors from affecting efficiency. If (process. processName = "System") & (process. processName = "Idle") continue; try {EmptyWorkingSet (process. handle);} catch {}}}}