溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解決C#定時(shí)器保活機(jī)制引起的內(nèi)存泄露問題

發(fā)布時(shí)間:2021-10-15 15:38:07 來源:億速云 閱讀:141 作者:柒染 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)如何解決C#定時(shí)器?;顧C(jī)制引起的內(nèi)存泄露問題,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

C# 中有三種定時(shí)器,System.Windows.Forms 中的定時(shí)器和 System.Timers.Timer 的工作方式是完全一樣的,所以,這里我們僅討論 System.Timers.Timer 和 System.Threading.Timer

1、定時(shí)器?;?/strong>

先來看一個(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í)例被?;盍恕?/p>

但多數(shù)時(shí)候這并不是我們想要的結(jié)果,這種結(jié)果導(dǎo)致的結(jié)果就是 內(nèi)存泄露,解決方案是:先將定時(shí)器 Dispose。

public class Foo : IDisposable{  ...  public void Dispose()  {    _timer.Dispose();  }}

一個(gè)很好的準(zhǔn)則是:如果類中的任何字段所賦的對象實(shí)現(xiàn)了IDisposable 接口,那么該類也應(yīng)當(dāng)實(shí)現(xiàn) IDisposable 接口。

在這個(gè)例子中,不止 Dispose 方法,Stop 方法和設(shè)置 AutoReset = false,都能起到釋放對象的目的。但是如果在 Stop 方法之后又調(diào)用了 Start 方法,那么對象依然會(huì)被保活,即便 Stop 之后進(jìn)行強(qiáng)制垃圾回收,也無法回收對象。

System.Timers.Timer System.Threading.Timer 的保活機(jī)制是類似的。

?;顧C(jī)制是由于定時(shí)器引用了實(shí)例中的方法,那么,如果定時(shí)器不引用實(shí)例中的方法呢?

2、不?;钕?System.Timers.Timer 和 System.Threading.Timer 的差異

要消除定時(shí)器對實(shí)例方法的引用也很簡單,將 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)委托。

關(guān)于如何解決C#定時(shí)器?;顧C(jī)制引起的內(nèi)存泄露問題就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI