项目比较大有时候会比较卡,虽然有GC自动清理机制,但是还是有不尽人意的地方。所以尝试在项目启动文件中,手动写了一个定时器,定时清理内存,加快项目运行速度。

 public class Program
{
[DllImport("psapi.dll")]
static extern int EmptyWorkingSet(IntPtr hwProc); //清理内存相关 static void Main()
{
//启动定时清理内存
SetTimer();
}
/// <summary>
/// 定时清理内存
/// </summary>
private static void SetTimer()
{
System.Timers.Timer aTimer = new System.Timers.Timer(); //初始化定时器
aTimer.Interval = ;//配置时间1分钟
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset = true;//每到指定时间Elapsed事件是到时间就触发
aTimer.Enabled = true; //指示 Timer 是否应引发 Elapsed 事件。
}
//定时器触发的处理事件
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{ //清理内存
GC.Collect();
GC.WaitForPendingFinalizers();
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
//以下系统进程没有权限,所以跳过,防止出错影响效率。
if ((process.ProcessName == "System") && (process.ProcessName == "Idle"))
continue;
try
{
EmptyWorkingSet(process.Handle);
}
catch
{
}
}
} }

C# System.Timers.Timer定时器的使用和定时自动清理内存应用的更多相关文章

  1. System.Timers.Timer(定时器)

    1.System.Timers命名空间下的Timer类.System.Timers.Timer类:定义一个System.Timers.Timer对象,然后绑定Elapsed事件,通过Start()方法 ...

  2. C# 定时器-System.Timers.Timer

    using Newtonsoft.Json; using Rafy; using Rafy.Domain; using System; using System.Collections.Generic ...

  3. System.Threading.Timer 定时器的用法

    System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此  .Net Framework 提供了5个重载的构造 ...

  4. [C#]System.Timers.Timer

    摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...

  5. 使用System.Timers.Timer类实现程序定时执行

    使用System.Timers.Timer类实现程序定时执行 在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和Sys ...

  6. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  7. 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别

    System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...

  8. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

    System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法http://space.itpub.net/1 ...

  9. C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题

    问题背景 C#小白,由于本公司IM系统服务端(java)是本人独立开发的,加上现在所在项目需要对接IM系统,于是IM的客户端(C#实现)对接工作就交给我了.于是C#小白的我天真的以为只要调用C#端的S ...

随机推荐

  1. bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...

  2. c++11之三: sizeof运算符 auto的优势 __func__预定义标识符

    在C++11中,对非静态成员变量使用sizeof操作是合法的. auto推导的一个最大优势就是在拥有初始化表达式的复杂类型变量声明时简化代码.如:std:vector<std::string&g ...

  3. Quartz 定时任务(含Redis)

    一  定时任务基础: MQ:Message Queue消息队列服务器:MSMQ.ActiveMQ.Redis等项目任务:确定邮件的发送,重置密码的发送(发送可能会很慢,而且有可能还需要重试),用消息队 ...

  4. POJ3111(最大化平均值)

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 8458   Accepted: 2163 Case Time ...

  5. 获取APK的package名和activity名

    使用 aapt dump badging + 需要安装的APK

  6. 怎样在win7中 安装Tomcat7.0

    Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器. 我们可以到官方网站下载Tomcat7 工具/原料 win7 Tomcat7.0 方法/步骤 1 在官网下载软件: ...

  7. docker 端口映射iptables: No chain/target/match by that name错误解决方法

    pkill docker iptables -t nat -F ifconfig docker0 down brctl delbr docker0 service docker restart

  8. shell脚本 回顾 小练习

    1.把/OPT目录下(包含子目录)下所有后缀为“.sh”的文件后缀变更为“.shell” 2.将A.B.C目录下的文件A1.A2.A3文件改名为A4.A5.A63.如何在vi模式下将文件中的aa字符串 ...

  9. JavaScript基本概念A

    简介 如果需要了解这些概念, 您应该熟悉 JS 的基本知识. 弱类型 在也无需绞尽脑汁觉得到底采用 float.double,int 还是 short 或是 long 还是 String.只需这样定义 ...

  10. 第九章 Servlet工作原理解析(待续)

    从 Servlet容器说起 创建 Servlet实例 Servlet体系结构 Servlet如何工作 Servlet中的Listener Filter如何工作 Servlet中的url-pattern