. NET cleanup memory, cleaning up memory usage methods

Source: Internet
Author: User

#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

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.