C # System. Timers. The use of the Timer and timed Automatic Cleaning of memory applications,

Source: Internet
Author: User

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 {}}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.