One of the restrictions of a Windows Phone 7 app is the maximum memory consumption of 90 MB (peak value). To check this you need to write your own memory usage monitor. You can use the follwing memory usage control, which can be started in the constructor of the app (optional with or without garbage collection).
1: // <summary>
2: /// Helper class for showing current memory usage
3: /// </summary>
4: public static class MemoryDiagnosticsControl
5: {
6: static Popup _popup;
7: static TextBlock _currentMemoryBlock;
8: static DispatcherTimer _timer;
9: static bool _forceGc;
10:
11: /// <summary>
12: /// Show the memory counter
13: /// </summary>
14: /// <param name="forceGc">Whether or not to do automatic garbage collection each tick</param>
15: public static void Start(bool forceGc)
16: {
17: _forceGc = forceGc;
18:
19: CreatePopup();
20: CreateTimer();
21: ShowPopup();
22: StartTimer();
23: }
24:
25: /// <summary>
26: /// Stop the memory counter
27: /// </summary>
28: public static void Stop()
29: {
30: HidePopup();
31: StopTimer();
32: }
33: /// <summary>
34: /// Show the popup
35: /// </summary>
36: static void ShowPopup()
37: {
38: _popup.IsOpen = true;
39: }
40:
41: static void StartTimer()
42: {
43: _timer.Start();
44: }
45:
46: static void CreateTimer()
47: {
48: if (_timer != null)
49: return;
50:
51: _timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(300)};
52: _timer.Tick += TimerTick;
53: }
54:
55: static void TimerTick(object sender, EventArgs e)
56: {
57: // call Garbage collector before getting memory usage
58: if (_forceGc)
59: GC.Collect();
60:
61: var mem = (long)DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage");
62: _currentMemoryBlock.Text = string.Format("{0:N}", mem / 1024);
63: }
64:
65: static void CreatePopup()
66: {
67: if (_popup != null)
68: return;
69:
70: _popup = new Popup();
71: var fontSize = (double)Application.Current.Resources["PhoneFontSizeSmall"] - 2;
72: var foreground = (Brush)Application.Current.Resources["PhoneForegroundBrush"];
73: var sp = new StackPanel { Orientation = Orientation.Horizontal, Background = (Brush)Application.Current.Resources["PhoneSemitransparentBrush"] };
74: _currentMemoryBlock = new TextBlock { Text = "---", FontSize = fontSize, Foreground = foreground };
75: sp.Children.Add(new TextBlock { Text = "Mem(kB): ", FontSize = fontSize, Foreground = foreground });
76: sp.Children.Add(_currentMemoryBlock);
77: sp.RenderTransform = new CompositeTransform { Rotation = 90, TranslateX = 480, TranslateY = 420, CenterX = 0, CenterY = 0 };
78: _popup.Child = sp;
79: }
80:
81: static void StopTimer()
82: {
83: _timer.Stop();
84: }
85:
86: static void HidePopup()
87: {
88: _popup.IsOpen = false;
89: }
90: }
Keine Kommentare:
Kommentar veröffentlichen