标签:
#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); } } #endregion
在系统中使用 SetProcessWorkingSetSize方法做了一个测试,调用该方法后,占用内存从80M降到2M.(TaskManager观察的结果)按 照知秋一叶的说法,这样调整WorkingSet,将有可能导致缺页中断,严重影响性能。 但是从使用的情况来看,没有发现这样的现象
public Form1() { InitializeComponent(); //每1分钟 检查一次 System.Timers.Timer timer = new System.Timers.Timer(60 * 1000); timer.Elapsed += timer_Elapsed; timer.Start(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //1 比较时间是否有效 HH:mm DateTime starttime = e.SignalTime; string time = ConfigurationManager.AppSettings["ClearMemory_PerdayTime"];//"05:00" string dtNow = 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 启动垃圾收集器 ClearMemory(); LogWarning(DateTime.Now.ToLongTimeString() + " :释放内存!"); } }
注意,引用命名空间:using System.Runtime.InteropServices;
标签:
原文地址:http://www.cnblogs.com/x-poior/p/5848562.html