in

WP7 memory monitoring tip

WP7 support guru Reed Robison shared this little gem, I thought I’d pass it on…

You can monitor memory pretty easily during debug…  
 
You can setup a timer in your app.xaml like this:
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1000d);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
 
Which can dump constant memory info using a handler like this:
        void timer_Tick(object sender, EventArgs e)
        {
            //GC.GetTotalMemory(true);
            long deviceTotalMemory = (long)Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceTotalMemory");
            long applicationCurrentMemoryUsage = (long)Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage");
            long applicationPeakMemoryUsage = (long)Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage");
 
            System.Diagnostics.Debug.WriteLine(DateTime.Now.ToLongTimeString());
            System.Diagnostics.Debug.WriteLine("Device Total : " + deviceTotalMemory.ToString());
            System.Diagnostics.Debug.WriteLine("App Current : " + applicationCurrentMemoryUsage.ToString());
            System.Diagnostics.Debug.WriteLine("App Peak : " + applicationPeakMemoryUsage.ToString());
        }
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

Remember that your app needs to run within a 90mb memory usage limit to pass certification. If you are blowing 90mb the runtime will shut you down when you run on a device. Unfortunately, the emulator (being an x86 compile) doesn’t. So worth monitoring your code just to make sure.

Digg This

» Trackbacks & Pingbacks

  1. Pingback from Twitter Trackbacks for WP7 memory monitoring tip - wotudo [wotudo.net] on Topsy.com
  2. Pingback from  Windows Phone 7 Developer Links « C Is For Coder

    Windows Phone 7 Developer Links « C Is For Coder — September 26, 2010 8:24 PM
  3. Pingback from  WP7 Detecting Memory Leaks « Simon Ransom's space

  4. Pingback from  Windows Phone 7 Xbox LIVE - Joining Us

    Windows Phone 7 Xbox LIVE - Joining Us — November 23, 2010 10:09 PM
Trackback url for this post:
http://wotudo.net/blogs/wotudo/trackback.ashx?PostID=2389

» Comments

  1. eeek - so much for the nice formatting....

    paulfo — September 23, 2010 3:56 PM

» Leave a Comment

(required) 
(optional)
(required) 

Submit