C# 定時(shí)器?;顧C(jī)制引起的內(nèi)存泄露問題解決
C# 中有三種定時(shí)器,System.Windows.Forms 中的定時(shí)器和 System.Timers.Timer 的工作方式是完全一樣的,所以,這里我們僅討論 System.Timers.Timer 和 System.Threading.Timer
1、定時(shí)器保活
先來看一個(gè)例子:
class Program
{
static void Main(string[] args)
{
Start();
GC.Collect();
Read();
}
static void Start()
{
Foo f = new Foo();
System.Threading.Thread.Sleep(5_000);
}
}
public class Foo
{
System.Timers.Timer _timer;
public Foo()
{
_timer = new System.Timers.Timer(1000);
_timer.Elapsed += timer_Elapsed;
_timer.Start();
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
WriteLine("System.Timers.Timer Elapsed.");
}
~Foo()
{
WriteLine("---------- End ----------");
}
}
運(yùn)行結(jié)果如下:
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
...
在 Start 方法結(jié)束后,F(xiàn)oo 實(shí)例已經(jīng)失去了作用域,按理說應(yīng)該被回收,但實(shí)際并沒有(因?yàn)槲鰳?gòu)函數(shù)沒有執(zhí)行,所以肯定實(shí)例未被回收)。
這就是定時(shí)器的 ?;顧C(jī)制,因?yàn)槎〞r(shí)器需要執(zhí)行 timer_Elapsed 方法,而該方法屬于 Foo 實(shí)例,所以 Foo 實(shí)例被保活了。
但多數(shù)時(shí)候這并不是我們想要的結(jié)果,這種結(jié)果導(dǎo)致的結(jié)果就是 內(nèi)存泄露,解決方案是:先將定時(shí)器 Dispose。
public class Foo : IDisposable
{
...
public void Dispose()
{
_timer.Dispose();
}
}
一個(gè)很好的準(zhǔn)則是:如果類中的任何字段所賦的對(duì)象實(shí)現(xiàn)了IDisposable 接口,那么該類也應(yīng)當(dāng)實(shí)現(xiàn) IDisposable 接口。
在這個(gè)例子中,不止 Dispose 方法,Stop 方法和設(shè)置 AutoReset = false,都能起到釋放對(duì)象的目的。但是如果在 Stop 方法之后又調(diào)用了 Start 方法,那么對(duì)象依然會(huì)被保活,即便 Stop 之后進(jìn)行強(qiáng)制垃圾回收,也無法回收對(duì)象。
System.Timers.Timer 和 System.Threading.Timer 的保活機(jī)制是類似的。
?;顧C(jī)制是由于定時(shí)器引用了實(shí)例中的方法,那么,如果定時(shí)器不引用實(shí)例中的方法呢?
2、不?;钕?System.Timers.Timer 和 System.Threading.Timer 的差異
要消除定時(shí)器對(duì)實(shí)例方法的引用也很簡(jiǎn)單,將 timer_Elapsed 方法改成 靜態(tài) 的就好了。(靜態(tài)方法屬于類而非實(shí)例。)
改成靜態(tài)方法后再次運(yùn)行示例,結(jié)果如下:
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
---------- End ----------
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
System.Timers.Timer Elapsed.
...
Foo 實(shí)例是被銷毀了(析構(gòu)函數(shù)已運(yùn)行,打印出了 End),但定時(shí)器還在執(zhí)行,這是為什么呢?
這是因?yàn)椋?NET Framework 會(huì)確保 System.Timers.Timer 的存活,即便其所屬實(shí)例已經(jīng)被銷毀回收。
如果改成 System.Threading.Timer,又會(huì)如何?
class Program
{
static void Main(string[] args)
{
Start();
GC.Collect();
Read();
}
static void Start()
{
Foo2 f2 = new Foo2();
System.Threading.Thread.Sleep(5_000);
}
}
public class Foo2
{
System.Threading.Timer _timer;
public Foo2()
{
_timer = new System.Threading.Timer(timerTick, null, 0, 1000);
}
static void timerTick(object state)
{
WriteLine("System.Threading.Timer Elapsed.");
}
~Foo2()
{
WriteLine("---------- End ----------");
}
}
注意,這里的 timerTick 方法是靜態(tài)的。運(yùn)行結(jié)果如下:
System.Threading.Timer Elapsed.
System.Threading.Timer Elapsed.
System.Threading.Timer Elapsed.
System.Threading.Timer Elapsed.
System.Threading.Timer Elapsed.
---------- End ----------
可見,隨著 Foo2 實(shí)例銷毀,_timer 也自動(dòng)停止并銷毀了。
這是因?yàn)椋?NET Framework 不會(huì)保存激活 System.Threading.Timer 的引用,而是直接引用回調(diào)委托。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android進(jìn)程?;钪嵘M(jìn)程優(yōu)先級(jí)
- Android 后臺(tái)運(yùn)行白名單實(shí)現(xiàn)?;?/a>
- node后端服務(wù)?;畹膶?shí)現(xiàn)
- 詳解Android 8.0以上系統(tǒng)應(yīng)用如何?;?/a>
- Android應(yīng)用?;顚?shí)踐詳解
- TCP 四種定時(shí)器(重傳定時(shí)器,堅(jiān)持計(jì)時(shí)器,?;疃〞r(shí)器,時(shí)間等待計(jì)時(shí)器)
- 詳解Android進(jìn)程保活的方法
- 詳解App?;顚?shí)現(xiàn)原理
相關(guān)文章
C#中DataTable 轉(zhuǎn)換為 Json的方法匯總(三種方法)
JavaScript Object Notation (Json)是一種輕量級(jí)的數(shù)據(jù)交換格式,下面小編給大家介紹三種方法實(shí)現(xiàn)DataTable轉(zhuǎn)換成 Json 對(duì)象,感興趣的朋友一起看看吧2016-11-11
C#中AutoResetEvent控制線程用法小結(jié)
本文主要來自一道面試題,由于之前對(duì)AutoResetEvent的概念比較模糊,面試題題目很簡(jiǎn)潔:兩個(gè)線程交替打印0~100的奇偶數(shù),你可以先動(dòng)手試試,我主要是嘗試在一個(gè)方法里面完成這個(gè)任務(wù),需要的朋友可以參考下2022-07-07
C# 關(guān)于AppDomain的一些總結(jié)
這篇文章主要介紹了C# 關(guān)于AppDomain的一些總結(jié),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02

