How to detect a 256 MB Windows Phone 7 device

Windows Phone logo

Last Wednesday I talked about a very good article from Nokia on the Best practices for developing on Windows Phone 7 Tango (256 MB devices). Today I’m going to show you how to detect if your application runs on a 512 MB device or 256 MB. Because you probably can’t have the same features in your applications for a “classic” device than on a “low-cost” one, you must be able (without coding a specific version of your application) to detect the capabilities of the current device and adjust the features accordingly.

So here is the code you have to implement at the start of your application. You can store the result (the variable IsLowMemoryDevice) into the application settings in order to avoid to call this method every time.

private void DetectMemoryDevice()
{
    try
    {
        // Gets the application working set limit. (if available)
        long memorySize = (long)DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit");

        // If the application has more than 90 MB
        // it indicates a 512-MB device.
        IsLowMemoryDevice = memorySize < 94371840L; // == 90 MB
    }
    catch (ArgumentOutOfRangeException)
    {
        // Windows Phone OS update (7.1.1)
        // is not installed, which indicates a 512-MB device. 
        IsLowMemoryDevice = false;
    }
}

Then you can use the variable IsLowMemoryDevice to activate or deactivate the features in your application.