1

我目前正在编写一个主要利用DispatcherTimer来模拟秒表功能的应用程序。当我的DispatcherTimer在应用程序中运行时,我的应用程序的内存使用量在不到10分钟的时间内就会增加到100MB,考虑到应用程序的功能有多简单,这特别奇怪。这通常不会成为问题,除非应用程序的内存使用量迅速增加,然后导致其崩溃并关闭。我查看了整个网络,并且一再发现有文章承认存在DispatcherTimer内存泄漏,但是对此内存泄漏的所有修复包括在不再需要时停止DispatcherTimer。我的内存泄漏发生在DispatcherTimer仍然需要时,而不是在意外运行时。我需要允许用户保持秒表运行,无论他们选择了多长时间,因此在不再需要时停止DispatcherTimer对我来说没有太大帮助。我曾尝试在我的TimerTick事件处理程序的末尾添加GC.Collect(),但是,这似乎也没有做太多。DispatcherTimer不断增加内存使用量,直到应用程序崩溃

public MainPage() 
    { 
     InitializeComponent(); 
     PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled; 
     Timer.Stop(); 
     Timer.Interval = new TimeSpan(0, 0, 1); 
     Timer.Tick += new EventHandler(TimerTick); 
     Loaded += new System.Windows.RoutedEventHandler(MainPage_Loaded);   

    } 

void TimerTick(object sender, EventArgs e) 
    { 

     timeSpan1 = DateTime.Now.Subtract(StartTimer); 
     timeSpan2 = DateTime.Now.Subtract(StartTimer2); 
     WatchHour.Text = timeSpan1.Hours.ToString(); 
     WatchMinute.Text = timeSpan1.Minutes.ToString(); 
     WatchSecond.Text = timeSpan1.Seconds.ToString(); 
     SecondaryHour.Text = timeSpan2.Hours.ToString(); 
     SecondaryMinute.Text = timeSpan2.Minutes.ToString(); 
     SecondarySecond.Text = timeSpan2.Seconds.ToString(); 


     if (WatchHour.Text.Length == 1) WatchHour.Text = "0" + WatchHour.Text; 
     if (WatchMinute.Text.Length == 1) WatchMinute.Text = "0" + WatchMinute.Text; 
     if (WatchSecond.Text.Length == 1) WatchSecond.Text = "0" + WatchSecond.Text; 


     if (SecondaryHour.Text.Length == 1) SecondaryHour.Text = "0" + SecondaryHour.Text; 
     if (SecondaryMinute.Text.Length == 1) SecondaryMinute.Text = "0" + SecondaryMinute.Text; 
     if (SecondarySecond.Text.Length == 1) SecondarySecond.Text = "0" + SecondarySecond.Text; 

    } 

这是我TimerTick事件处理程序,有点我的构造函数的MainPage的,存在于事件处理程序的文本框显示从开始秒表经过的时间。我在这里做了一些特别错误的事情,导致内存如此巨大的增加?我以前认为这个问题是因为TextBoxes默认缓存了其以前的内容,加上由于秒表功能导致的文本快速变化,但是,在完全从应用程序中删除TextBox并分析它之后,我确信它们不是这个问题。如上所述,在此TimerTick处理程序的末尾添加GC.Collect()并没有做任何事情来减少我的内存使用量。有没有人有一个想法,我可以通过DispatcherTimer减少我的内存使用量,也许通过某种方式操纵GC功能来实际工作?

在此先感谢!

+0

如何在tick事件开始时停止计时器,并在最后再次启动计时器? –

+1

我不认为上述代码存在问题。我怀疑其他地方的罪魁祸首。顺便说一句,使用'WatchHour.Text = timeSpan1.Hours。ToString(“00”);'根据需要实现正确的格式化 –

+0

如果让应用程序运行,或者内存在某个点停止增加,你会崩溃吗? –

回答

0

我终于隔离了内存泄漏的核心,问题不在于我的DispatcherTimer,而在于我的AdRotator控件。问题已在AdRotator开发人员身上发现,我目前正在使用不同的Ad控件,直到问题得到解决。

谢谢大家的帮助,我真的很感谢你的时间和精力!

+0

尝试将此问题标记为已回答... –

+0

它说我需要等待2个小时才能将我自己的答案标记为我接受的答案:S – user2637236

0

为什么要在计时器滴答事件之外声明时间跨度1和时间跨度2?请问内存看起来更好,如果你的事件处理程序

+0

它看起来像内存使用是一样的,但是,你确实帮助简化了我的代码! – user2637236

0

能否请你试试下面的代码片段里面创建它,

步骤:1 添加按钮和文本块在XAML第一。

第2步:使用 以下命名空间:

using System.Diagnostics; // for Stopwatch API Access 

步骤:3使用下面提到的代码片段:

public partial class WmDevStopWatch : PhoneApplicationPage 
    { 
     Stopwatch stopWatch = new Stopwatch(); 
     DispatcherTimer oTimer = new DispatcherTimer(); 

     public WmDevStopWatch() 
     { 
      InitializeComponent(); 

      oTimer.Interval = new TimeSpan(0, 0, 0, 0, 1); 
      oTimer.Tick += new EventHandler(TimerTick); 

     } 

     void TimerTick(object sender, EventArgs e) 
     { 
      Dispatcher.BeginInvoke(() => 
      { 
       string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", 
        stopWatch.Elapsed.Hours, stopWatch.Elapsed.Minutes, stopWatch.Elapsed.Seconds, 
        stopWatch.Elapsed.Milliseconds/10); 

       textBlock1.Text = elapsedTime; 
      }); 
     } 


     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      stopWatch.Start(); 
      oTimer.Start(); 
     } 
    } 

希望它为你工作。

让我知道你的意见。

相关问题