1

I am developing windows Phone application. In my application am using Longlistselector and loading long lists with images using MVVM. Also in the details page after lists there are few high res images.

In my application am going from Page 1 to Page 2 and page 2 to page 1 n number of times. Application is crashing in back navigation giving ArgumentOutOfRangeException and OutOfMemoryException.

I have done the Windows phone application analysis. Its showing GC events are getting called frequently. However it does not show at what point application crashes and how to reduce or how to clean up memory used by application.

If anyone knows about memory cleanup and how to prevent memory leaks please let me know.

Manmath
  • 66
  • 6

2 Answers2

0

The biggest gotchas for memory leaks are event handlers not being released. Whenever you subscribe to an event, you also need to unsubscribe the event when the updates are no longer needed. I'd guess that your page 2 isn't actually going away when you navigate back to page 1. I believe there are a pair of overloaded methods (OnNavigatingTo and OnNavigatingFrom, I think) that can help you manage this.

Another way is to use a WeakReference and manually pass along any data with an EventAggregator. Probably the easiest one to use is part of the MVVM Light Toolkit (the Messenger). This Weak Reference won't have your event senders hold references to your subscribers.

Christopher Stevenson
  • 2,843
  • 20
  • 25
  • 1
    Using memory profiling I have observed that there are more than one instances of the Page 1 and Page 2. How to delete these instances? These instances live in memory for app lifetime. Also the ViewModels associated with these instances are not getting collected. How do i achieve this? – Manmath Jun 19 '13 at 06:26
  • This will take some work, but you need to carefully go through what objects have references to your pages and set them to null. – Christopher Stevenson Jun 20 '13 at 15:56
  • That is, when navigating away from the page – Christopher Stevenson Jun 20 '13 at 16:02
  • Here's a bit of advice: http://stackoverflow.com/questions/600757/strategies-for-tracking-down-memory-leaks-when-youve-done-everything-wrong – Christopher Stevenson Jun 20 '13 at 16:03
0

You also have to remember that windows phone is a new paradigm. You do not navigate from page to page, but rather to a page and back to the previous page. If you are going forward all of the time, you will have new instances of the pages in the navigation stack.