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:
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
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:
- <TextBlock x:Name="AppMemoryUsage" FontSize="24"/>
- <TextBlock x:Name="AppMemoryUsageLimit" Margin="0,30,0,-25" FontSize="24" />
- <TextBlock x:Name="AppMemoryUsageLevel" Margin="0,58,0,-58" FontSize="24" />
- <Button Content="Memory Status" Click="Memory_Click" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,108,0,475"/>
- <Button Content="Download" Click="Download_Click" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,159,0,424"/>
- <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.
-
- ulong AppMemoryUsageUlong = MemoryManager.AppMemoryUsage;
-
-
- ulong AppMemoryUsageLimitUlong = MemoryManager.AppMemoryUsageLimit;
-
- AppMemoryUsageUlong /= 1024 * 1024;
- AppMemoryUsageLimitUlong /= 1024 * 1024;
- AppMemoryUsage.Text = "App memory uage - " + AppMemoryUsageUlong.ToString();
- AppMemoryUsageLimit.Text = "App memory usage limit - " + AppMemoryUsageLimitUlong.ToString();
-
-
- 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 :
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:
- 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:
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.