【发布时间】:2019-10-31 16:59:28
【问题描述】:
我在我的项目中使用了 Timer,但每次运行 myTimer.Tick 时,myApp 都会使用比以前更多的内存。 开始时使用了 63MB 的 RAM,30 分钟后它使用了 700MB。
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
DispatcherTimer tmShowAlarm = new DispatcherTimer();
tmShowAlarm.Tick += new EventHandler(ShowAlarm);
tmShowAlarm.Interval = new TimeSpan(1000);
tmShowAlarm.Start();
}
private void ShowAlarm(object Sender, EventArgs e)
{
string strDate = "2019/10/10";
DatabaseContext oDatabaseContext = null;
try
{
oDatabaseContext = new DatabaseContext();
var varNote = oDatabaseContext.Notes.Where(_note => _note.NoteDate.CompareTo(strDate ) < 0);
dgShowResult.ItemsSource = null;
if (varNote.Count() > 0)
{
dgShowResult.ItemsSource = varNote.ToList();
dgShowResult.Visibility = Visibility.Visible;
}
else
{
dgShowResult.Visibility = Visibility.Hidden;
}
varNote = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
oDatabaseContext.Dispose();
oDatabaseContext = null;
}
}
当我删除 dgShowResult.ItemsSource = varNote.ToList(); 它不会使用更多内存。
【问题讨论】:
-
只有 700MB 的 RAM 是如何“满”的?这不重要!垃圾收集器可能正在睡觉,因为真的没有什么可做的......
-
您从哪里得到“它使用了 700MB”的想法?从任务管理器? That does not show how much memory is in use。如果您想知道您的应用程序使用了多少内存,您必须运行内存分析器。
-
在任务管理器中显示 RAM 和 CPU