Search Share Home Devices Settings


[#11] WP7Tips: tenere sotto controllo la memoria

(c) lorenzo barbieri - 03/01/2011

In alcuni casi le applicazioni WP7 iniziano ad occupare “troppa” memoria, per via di immagini troppo grandi, listbox non virtualizzate troppo grandi, video, audio, memory leak, etc…

Può essere comodo “tenere sott’occhio” la memoria (che non deve superare i 90Mb, pena la non certificazione sul Marketplace).

Si può fare in due modi. Il primo prevede di stampare i dati sull’occupazione della memoria direttamente nell’Output Window di Visual Studio, aggiungendo queste righe nell’App.xaml.cs :

public App()
{
… 
     #if (DEBUG)
          if (System.Diagnostics.Debugger.IsAttached)
          {
                     System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
                     timer.Interval = TimeSpan.FromMilliseconds(1000d);
                     timer.Tick += new EventHandler(timer_Tick);
                     timer.Start();

           }
      #endif

}

[Conditional("DEBUG")]
void timer_Tick(object sender, EventArgs e)
{
            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());
}

 

Il secondo modo li stampa a video come i counter del framerate presenti di default nei progetti.

Scegliete quello che preferite.

Tags: WP7 WP7Tips

Comments disabled in this version of the blog.