title: Memory leak , timer in WPF
body: <p>I have this problem. I develop WPF app with MVVM architecture. As MVVM framework I use Caliburn.Micro and on service  injection I use MEF.</p>
  
  <p>In view model class I use Dispatcher Timer which call every 3 seconds service method. This service method send POST request on server and parse HTML response / deserialize JSON string with JSON.NET. </p>
  
  <p>This method return observable dictionary, is type : string, Friend.</p>
  
  <p>Friend class consist only string properties and also one image as BitmapImage type.</p>
  
  <p><strong>Here is service method:</strong></p>
  
  <pre><code>     public MyObservableDictionary&lt;string, Friend&gt; LoadFriends(Account account)
          {
  
              var friends = new MyObservableDictionary&lt;string, Friend&gt;();
              var sortedFriends = new MyObservableDictionary&lt;string, Friend&gt;();
  
              const string allData = "&amp;allData=1";
  
              var htmlStringResult = new StringBuilder();
  
              htmlStringResult = "GET HTML RESPOSE"
  
              if (htmlStringResult.Length &gt; 3 &amp;&amp; htmlStringResult.ToString() != "false")
              {
                  try
                  {
                      var jsonString = new StringBuilder();
                      jsonString.Append(htmlStringResult.Replace(@"s_", "m_"));
  
                      var friendsAsStringArray = JsonConvert.DeserializeObject&lt;MyObservableDictionary&lt;string, string[]&gt;&gt;(jsonString.ToString());
  
                      foreach (var friend in friendsAsStringArray)
                      {
                          var item = new KeyValuePair&lt;string, Friend&gt;(friend.Key, new Friend
                          {
                              IdUser = friend.Value[0],
                              Nick = friend.Value[1],
                              SefNick = friend.Value[1],
                              Status = int.Parse(friend.Value[2]),
                              Photo = friend.Value[3],
                              Sex = int.Parse(friend.Value[4]),
                              IsFriend = Convert.ToBoolean(int.Parse(friend.Value[5]) * -1)
                          });
                          friends.Add(item);
                      }
  
  
  //sort item in dictionary  A-Z
                      var query = friends.OrderByDescending(f =&gt; f.Value.Status).ThenBy(f =&gt; f.Value.Nick);
  
                      foreach (var keyValuePair in query)
                      {
                          sortedFriends.Add(keyValuePair.Key, keyValuePair.Value);
                      }
  
                      //CLEAN OLD DATA
                      friends.Clear();
                  }
                  catch (Exception exception)
                  {
                      throw exception;
                  }
                  return sortedFriends;
              }
              return new MyObservableDictionary&lt;string, Friend&gt;();
          }
  </code></pre>
  
  <p>In view model class I bind observable dictionary on listbox.  In this class I call in timer every 3 seconds service mehod <strong>LoadFriend</strong>.</p>
  
  <p>I returns fresh data in observable dictionary and with this data I refresh dictionary which is bind on listbox.</p>
  
  <p><strong>Here is view model class:</strong></p>
  
  <pre><code>[Export(typeof(IMessengerViewModel))]
  public class MessengerViewModel : Screen, IViewModelIdentity,
      IMessengerViewModel, IHandle&lt;Rp&gt;, IHandle&lt;string&gt;
  {
      private IPokecService _service;
  
      private MyObservableDictionary&lt;string, Friend&gt; _friends;
      private MyObservableDictionary&lt;string, Friend&gt; _freshFriends;
  
      private DispatcherTimer _dispatcherTimer;
  
      [ImportingConstructor]
      public MessengerViewModel(IPokecService service)
      {
          _service = service;
          _dispatcherTimer = new DispatcherTimer();
          _dispatcherTimer.Tick += DispatcherTimer_Tick;
          _dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
          _dispatcherTimer.Start();
  
      }
  
  
      #region Timer
  
      /// &lt;summary&gt;
      /// Refresh contact list, check for new messages
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;&lt;/param&gt;
      /// &lt;param name="eventArgs"&gt;&lt;/param&gt;
      private void DispatcherTimer_Tick(object sender, EventArgs eventArgs)
      {
          //retrieve new data from server
          _freshFriends = _service.LoadFriends(Account);
  
          _friends.Clear();
  
  
          //refresh dictionary
          foreach (var freshFriend in _freshFriends)
          {
              _friends.Add(freshFriend);
          }
      }
  
      #endregion
  }
  </code></pre>
  
  <p>I make a simple test:</p>
  
  <ul>
  <li>run app with timer</li>
  <li>run app without timer</li>
  <li>with timer but empty logic for
  timer_tick method</li>
  </ul>
  
  <p>I check values in task manager and in ANTS Memory profiler, here are value:</p>
  
  <p><strong>Task manager process:</strong></p>
  
  <ul>
  <li>App with timer
  <ul>
  <li>after start : 40,133K</li>
  <li>after 5 minutes: 70,261K</li>
  <li>after 10minutes: 74,288K</li>
  </ul></li>
  </ul>
  
  <hr>
  
  <ul>
  <li>App without timer
  <ul>
  <li>after start : 37,488K</li>
  <li>after 5 minutes: 37,412K</li>
  <li>after 10minutes: 37,760K</li>
  </ul></li>
  </ul>
  
  <hr>
  
  <ul>
  <li>App with timer but empty logic 
  <ul>
  <li>after start : 37,474K</li>
  <li>after 5 minutes:37,340K</li>
  <li>after 10minutes: 37,476k</li>
  </ul></li>
  </ul>
  
  <p><strong>ANTS Memory profiler - Privates bytes</strong></p>
  
  <ul>
  <li>App with timer
  <ul>
  <li>after start: 73,132MB</li>
  <li>after 5 minutes: 97,72MB</li>
  <li>after 10minutes: 100,12MB</li>
  </ul></li>
  </ul>
  
  <hr>
  
  <ul>
  <li>App without timer
  <ul>
  <li>after start : 67,24MB</li>
  <li>after 5 minutes: 66,21MB</li>
  <li>after 10minutes: 66,21MB</li>
  </ul></li>
  </ul>
  
  <hr>
  
  <ul>
  <li>App with timer but empty logic 
  <ul>
  <li>after start : 67,3MB</li>
  <li>after 5 minutes: 66,32MB</li>
  <li>after 10minutes:66,41MB</li>
  </ul></li>
  </ul>
  
  <p>I don’t know that this values are normal. Maybe I have memory leak in app.</p>
  
  <p>My first idea was check images in Friend class, but image is store in BitmapImage class. This class doesn’t implements interface IDisposable, so I can’t call method Dispose on old items in dictionary.</p>
  
  <p>Maximum of privates bytes was 124,12 MB I think its quite enough.</p>
  
  <p>I never handling with memory usage in .NET, I don’t know what can be wrong.</p>
  
  <p>Thank for help, if someone explain me this problem I would be very gratefull him/her.</p>
  
  <p>I think call in my timer garbage collector, but I think it’s goog idea.</p>
  
  <p>Size of one image is 5-10kB. And in dictionary is about 20-30 items.</p>
  
  <p><strong>I download image from web, image uri is URL.</strong></p>
  
  <pre><code> public BitmapImage ProfilePhoto ...{ get; set; }
  
              private BitmapImage CreateProfilePhoto()
              {
                  var img = new BitmapImage();
                  img.BeginInit();
  
                  img.UriSource = Photo == "0" ? DefaultPhoto.GetDefaultPhoto(Sex) : new Uri(PhotoURL, UriKind.Absolute);
  
                  img.EndInit();
  
                  return img;
              }
  
      ....
  
              ProfilePhoto = CreateProfilePhoto();
  </code></pre>