Freigeben über


WP7 Perf Tip #5: Check your memory usage

Two for the price of one today!

Take Aways:

  1. Make sure your memory usage is below 90MB.
  2. Always check your memory usage while you're developing your app (preferably on device) by using the following code:
 long deviceTotalMemory = (long)DeviceExtendedProperties.GetValue("DeviceTotalMemory");
long applicationCurrentMemoryUsage = (long)DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage");
long applicationPeakMemoryUsage = (long)DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage");

Why?

The Windows Phone 7 Application Certification Requirements specify (as of 28th of September 2010):

5.2.5 Memory Consumption

The application must not exceed 90 MB of RAM usage. However, on devices that have more than 256 MB of memory, an application can exceed 90 MB of RAM usage. The DeviceExtendedProperties class can be used to query the amount of memory on the device and modify the application behavior at runtime to take advantage of additional memory. For more information, see the DeviceExtendedProperties class in MSDN.

Since you have a hard limit of 90MB (exceedable in some cases) it's worth checking that you're not getting anywhere near that while your app is running. The code above will give you the Peak (which is important to make sure you've never crossed the 90MB barrier) and the current memory. If you call that out every now and then you should be able to get a pretty good idea of how your memory fluctuates during the lifetime of the app.

Can I Go Above 90MB?

Sure - but be careful! If you're app is memory intensive and would benefit from a little extra breathing room, then feel free to add some extra logic which dynamically loads more items into memory until a certain limit is hit. Note though that there is no defined limit above the 90MB barrier, so expect this to be clarified in the future.

Kudos

Kudos go to Stefan Wick, Principal Test Manager for pushing this code

Comments

  • Anonymous
    July 22, 2011
    So what happens when your app goes over the 90MB mark? does that app just crash? Do you get any sort of warning within the code?

  • Anonymous
    July 22, 2011
    @MattM Your app will continue consuming memory until there's none left (for example, most devices have 512MB of memory so you can consume more without running out of memory), at which point your app will crash with an OutOfMemory Exception, which usually can't be properly handled since there's not much you can do at that point. Hope that helps!