Memory Usage of Your App - Windows Phone 8.1

For Mobile application performance the following are very important:

  • Making your app available on the widest range of WP devices possible
     
  • Ensure users have a great experience with your app on all devices and at all times

Windows Phone has different sizes of memory right now, like 512 MB, 1 GB and 2 GB . Typically, a Phone with 512 MB of memory is considered to be a low-memory device.

Memory Consumption of App

An application must not exceed 185 MB of of RAM usage, except on devices that have more than 512 MB of memory. We are discussing the latest release of Windows Phone 8.1 where the memory usage limit is increased slightly for various from devices.

Here is the table of memory as an image:

Memory Consumption

You can use the MemoryManager Class to get how much of memory your app is using right now and what your app's memory usage limit is. In this article we are focusing on Windows Phone 8.1 (Silverlight 8.1 and Windows Runtime 8.1) so we need Visual Studio 2013.

Create a Project that targets Widnows Phone 8.1

Creating Blank App

Now we will see how much memory our app is consuming from the Device's RAM memory. For this I created the following controls in an XAML Page:

  1. <TextBlock x:Name="AppMemoryUsage" FontSize="24"/>   
  2. <TextBlock x:Name="AppMemoryUsageLimit" Margin="0,30,0,-25" FontSize="24" />   
  3. <TextBlock x:Name="AppMemoryUsageLevel" Margin="0,58,0,-58" FontSize="24" />   
  4. <Button Content="Memory Status" Click="Memory_Click" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,108,0,475"/>   
  5. <Button Content="Download" Click="Download_Click" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,159,0,424"/>   
  6. <Image x:Name="image" Height="400" Margin="0,234,0,6" /> 

In MainPage.xaml.cs we will add the MemoryManager class and this class is invoked from the Windows.System namespace.

So I wrote this code to execute in the Memory_Click Button event method to get the Current Memory details and display it in TextBlocks.

  1. // Gets the app's current memory usage   
  2. ulong AppMemoryUsageUlong = MemoryManager.AppMemoryUsage;   
  3.   
  4. // Gets the app's memory usage limit   
  5. ulong AppMemoryUsageLimitUlong = MemoryManager.AppMemoryUsageLimit;   
  6.   
  7. AppMemoryUsageUlong /= 1024 * 1024;   
  8. AppMemoryUsageLimitUlong /= 1024 * 1024;   
  9. AppMemoryUsage.Text = "App memory uage - " + AppMemoryUsageUlong.ToString();   
  10. AppMemoryUsageLimit.Text = "App memory usage limit - " + AppMemoryUsageLimitUlong.ToString();   
  11.   
  12. // Gets the app's memory usage level whether low or medium or high   
  13. AppMemoryUsageLevel.Text = "App memory usage level - " + MemoryManager.AppMemoryUsageLevel.ToString(); 

Since AppMemoryUsage and AppMemoryUsageLimit returns a value in Bytes I converted the values to MegaBytes and displayed them in TextBlocks. Mostly the result will be like this :

Xaml Page

Increase the Memory Usage of App

Now I download an image to see how usage memory will be impacted. In Download_Click I wrote the following code:

  1. image.Source = new BitmapImage(new Uri("http://freebigpictures.com/wp-content/uploads/2009/09/cloudy-field.jpg", UriKind.Absolute)); 

Since the image size is large it will take a few seconds to download and display in the image control. Now check the memory status details by tapping on the MemoryStatus button. The Screenshot will be like this:

App Memory Use

By looking at the textblock results you can determine the Current memory usage has increased to 9 MB. Since the image size is 2 MB and it displayed in our image control the memory of RAM usage is increased.

We can use the advantage of events [ AppMemoryUsageIncreased and AppMemoryUsageDecreased ] that will be raised when the app's memory consumption is increased/decreased.

By knowing these details we can try to reduce the current usage of app memory because if the app exceeds the usage limit app it will crash by raising the " Out of memory " exception and Microsoft will refuse to certify your app if you exceed the memory limits.

Up Next
    Ebook Download
    View all
    Learn
    View all