Following Brendan’s post regarding memory usage in Windows Phone 7 applications I thought I’d take a look at some very basic scenarios to get a better feel for where memory is consumed.
Let’s start with a simple application, and I mean simple. Start a new project and basically rip out anything that isn’t required so that you end up with a blank page. In my case I even ripped out the splash screen. I’ve got a simple timer that executes every two seconds and reports current and maximum memory usage for the application. Here’s the first chart, followed by an explanation of each series.
– Single page (GC): This is the application with just the empty MainPage.xaml. The GC indicates that GC.Collect is called before querying the memory usage.
– Single Page (1) and (2): This is the application with just the empty MainPage.xaml but without the GC.Collect calls. You can only see two of these series as the others are identical so aren’t visible as they sit behind the other two.
– Two Pages: This is the application with a second (again empty) page added. This page is not navigated to.
– Two Pages (with 1 navigation): This is the application with a second page that is navigated to after approximately 6 seconds.
– Two Pages (with return navigation): This is the application with a second page that is navigated to after approximately 6 seconds and then navigated back to the MainPage at around the 12 second mark.
Whilst moderately interesting, there isn’t much useful information contained here. Let’s move right along to the next set of lines:
– Two Pages (iterative nav): This is the same application this time we’re navigating back and forth between the MainPage and the second page every approximately 6 seconds.
– Two Pages (iterative nav) (GC): Same again, but this time we’re calling GC.Collect before each query of the memory usage (this stops at 200 second mark as I got bored because the line wasn’t going anywhere…)
Ok, so now this is interesting. Remembering that your application has to sit below the 90Mb threshold for Marketplace certification it is apparent that an occasional call to GC.Collect may be in order to free up some resources. This is completely in contrast to the recommendation that you should never call GC.Collect!