-2

I'm using a Timer in my project, but every time that run myTimer.Tick, myApp use more memory than before. At start in used 63MB of RAM after 30 minutes it used 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;
    }

}

When I remove dgShowResult.ItemsSource = varNote.ToList(); it doesn't use more memory.

Hossein Tavakoli
  • 113
  • 1
  • 2
  • 10
  • How is you RAM 'full' with only 700MB?? This is nothing! Garbage collector is probably sleeping because there isn't anything to do really.. – TaW Oct 31 '19 at 17:05
  • 2
    Where are you getting the idea “it used 700MB”? From Task Manager? [That does not show how much memory is in use](https://stackoverflow.com/questions/12017437/). If you want to know how much memory your application uses you must run a memory profiler. – Dour High Arch Oct 31 '19 at 17:17
  • In Task Manager show RAM and CPU – Hossein Tavakoli Oct 31 '19 at 17:33

2 Answers2

0

This will not solve your problem but you may prefer employ the recommended using standard pattern instead of manually calling Dispose():

using (var oDatabaseContext = new DatabaseContext() )
  try
  {
    //
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }

For the increased memory, you must know that the GC.Collect() can run at any some time in background.

Hence what you call a problem is normal: managed memories spaces are not released when you lost lasts references of objects, they are only marked as to be released later when needed, usually if the system has low memory available.

So you can call it explicitally at the end of the method: it doesn't guarantee that it runs but on Windows it generally runs when you call it while causing a little lag.

  finally
  {
    GC.Collect();
  }

Therefore perhaps the memory allocated by ToList() that is no more used will be freed.

https://learn.microsoft.com/dotnet/api/system.gc.collect#System_GC_Collect

  • What type of memory do you inspect and how? –  Oct 31 '19 at 18:35
  • in Task Manager show an application using how much of RAM and CPU, after 30 minutes myApp used 700MB RAM and Computer is starting Hang up. sorry I cant speaking English very well. – Hossein Tavakoli Oct 31 '19 at 19:04
  • Column name is Memory, When I remove dgShowResult.ItemsSource = varNote.ToList(); it doesn't use more memory – Hossein Tavakoli Oct 31 '19 at 19:21
  • You should be able to choose columns to display more detailled info... Because there is many types of memory like WorkingSet, PrivateMemory, PagedMemory... current and peak: https://stackoverflow.com/questions/842585/getting-a-processs-ram-usage and https://docs.microsoft.com/dotnet/api/system.diagnostics.process and https://docs.microsoft.com/en-us/windows/win32/psapi/process-memory-usage-information. You should consider WorkingSet without Peak. If you can't improve your Task Manager, you can use a method in your app or *Process Hacker* for example. Remember that GC is not on your control. –  Oct 31 '19 at 19:37
-1

It solved, A DispatcherTimer will keep an object alive, I should use System.Timers.Timer.

Hossein Tavakoli
  • 113
  • 1
  • 2
  • 10
  • You should definitely use DispatcherTimer in a WPF application, especially when you want to update the UI from the Tick handler. – Clemens Nov 24 '21 at 21:37