Monitoring Memory Usage on Windows Phone 7
Frame rate counters are available (for Silverlight at least) right out of the box on Windows Phone 7 (in fact they’re enabled by default in the standard VS project templates when a debugger is attached):
// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
// Display the current frame rate counters.
Application.Current.Host.Settings.EnableFrameRateCounter = true;
Monitoring the memory usage of your application requires a little more work (not much). Access to memory usage information is provided through the Microsoft.Phone.Info.DeviceExtendedProperties class in Microsoft.Phone.dll.
This class lets you query various properties of the device including manufacturer, name, unique id and what we’re interested in here: total memory and current and peak memory usage.
DeviceTotalMemory |
A long integer. |
The device’s physical RAM size in bytes. This value will be less than the actual amount of device memory, but can be used for determining memory consumption requirements. |
ApplicationCurrentMemoryUsage |
A long integer. |
The current application’s memory usage in bytes. |
ApplicationPeakMemoryUsage |
A long integer. |
The current application’s peak memory usage in bytes. |
So, something like this will suffice for out purposes.
const string total = "DeviceTotalMemory";
const string current = "ApplicationCurrentMemoryUsage";
const string peak = "ApplicationPeakMemoryUsage";
long totalBytes =
(long)DeviceExtendedProperties.GetValue(total);
long currentBytes =
(long)DeviceExtendedProperties.GetValue(current);
long peakBytes =
(long)DeviceExtendedProperties.GetValue(peak);
totalMemory.Text = totalBytes.ToString();
currentMemory.Text = currentBytes.ToString();
peakMemory.Text = peakBytes.ToString();
Where totalMemory, currentMemory and peakMemory are TextBlocks for displaying the results.
Alternatively (and somewhat easier still) Garry McGlennon has written a helper – simply add a reference to his dll and with one line of code you have the memory counters visible (and regularly updated) in your app.
Note: I had to change SystemTray.IsVisible to False to see the counters (it’s set to True by default in the VS project template).
Comments
- Anonymous
December 21, 2010
I have a Dell Venue and a Focus. Both Devices are supposed to have 512MB of RAM. The Focus displays 475MB and the Venue displays 225. Why would the DELL be so low. Does it only contain 256MB?