项目比较大有时候会比较卡,虽然有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. LR录制https协议设置方法

    1.New一个脚本,option --> port mapping --> 选择第二个  WinNet level data 2.Run-time Setting --> Prefe ...

  2. SpringCloud微服务实战——第二章Springboot

    Spring Boot项目工程 src/main/java:主程序入口HelloApplication,可以通过直接运行该类来启动Spring Boot应用. src/main/resources:配 ...

  3. Win10 Bash更改默认用户

    Win10已经支持Ubuntu的Bash了. 在cmd中输入bash就可以进入bash界面.但此时是普通用户登录的. 如果希望更改默认的登录用户,可以在cmd中更改. 具体的命令是: lxrun /s ...

  4. 蓝桥杯 历届试题 PREV-1 核桃的数量

    历届试题 核桃的数量   时间限制:1.0s   内存限制:256.0MB 问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑 ...

  5. MySQL 更新失效

    create table t(id int not null PRIMARY key,c int default null) engine=innodb;insert into t(id,c)valu ...

  6. HTML元素(包括HTML5)的标签、属性及事件大全

    1.标签列表 标签 描述 <!–…–> 定义注释. <!DOCTYPE> 定义文档类型. <a> 定义超链接. <abbr> 定义缩写. <acr ...

  7. Linux_oracle命令大全(转)

    Linux_oracle命令大全 一,启动 1.#su - oracle              切换到oracle用户且切换到它的环境 2.$lsnrctl status           查看 ...

  8. [codeforces126B]Password

    解题关键:KMP算法中NEXT数组的理解. #include<bits/stdc++.h> #define maxn 1000006 using namespace std; typede ...

  9. c++的单例模式及c++11对单例模式的优化

    单例模式 单例模式,可以说设计模式中最常应用的一种模式了,据说也是面试官最喜欢的题目.但是如果没有学过设计模式的人,可能不会想到要去应用单例模式,面对单例模式适用的情况,可能会优先考虑使用全局或者静态 ...

  10. Java3D读取3DMax模型并实现鼠标拖拽、旋转、滚轮缩放等功能

    /**-------------------------------------------------代码区--------------------------------------------- ...