zoukankan      html  css  js  c++  java
  • windows phone 开发模拟器调试实时监测内存

    方便开发人员监测应用运行内存使用状况,开启后会在模拟器右侧显示实时数据
     
     
    一、新建工具类
      public static class MemoryDiagnosticsHelper
      {
        static Popup popup;
        static TextBlock currentMemoryBlock;
        static DispatcherTimer timer;
        static bool forceGc;
     
        public static void Start(bool forceGc)
        {
          MemoryDiagnosticsHelper.forceGc = forceGc;
     
          CreatePopup();
          CreateTimer();
          ShowPopup();
          StartTimer();
        }
     
        private static void ShowPopup()
        {
          popup.IsOpen = true;
        }
     
        private static void StartTimer()
        {
          timer.Start();
        }
     
        private static void CreateTimer()
        {
          if (timer != null)
            return;
     
          timer = new DispatcherTimer();
          timer.Interval = TimeSpan.FromMilliseconds(300);
          timer.Tick += new EventHandler(timer_Tick);
        }
     
        static void timer_Tick(object sender, EventArgs e)
        {
          if (forceGc)
            GC.Collect();
     
          long mem = (long)DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage");
          currentMemoryBlock.Text = string.Format("{0:N}", mem / 1024);
        }
     
        private static void CreatePopup()
        {
          if (popup != null)
            return;
     
          popup = new Popup();
          double fontSize = (double)App.Current.Resources["PhoneFontSizeSmall"] - 2;
          Brush foreground = (Brush)App.Current.Resources["PhoneForegroundBrush"];
          StackPanel sp = new StackPanel { Orientation = Orientation.Horizontal, Background = (Brush)App.Current.Resources["PhoneSemitransparentBrush"] };
          currentMemoryBlock = new TextBlock { Text = "---", FontSize = fontSize, Foreground = foreground };
          sp.Children.Add(new TextBlock { Text = "Mem(k): ", FontSize = fontSize, Foreground = foreground });
          sp.Children.Add(currentMemoryBlock);
          sp.RenderTransform = new CompositeTransform { Rotation = 90, TranslateX = 480, TranslateY = 420, CenterX = 0, CenterY = 0 };
          popup.Child = sp;
        }
     
        public static void Stop()
        {
          HidePopup();
          StopTimer();
        }
     
        private static void StopTimer()
        {
          timer.Stop();
        }
     
        private static void HidePopup()
        {
          popup.IsOpen = false;
        }
      }
     
    二、在应用初始化时开启监测
    App.cs中
     public App()
        {     
    #if DEBUG
          // Display the current frame rate counters.
          Application.Current.Host.Settings.EnableFrameRateCounter = true;
          MemoryDiagnosticsHelper.Start(true);
    #endif
    }
  • 相关阅读:
    消除ie上的:为了有利于保护安全性,IE已限制此网页运行可以访问计算机的脚本或 ActiveX 控件
    JS代码放在head和body中的区别
    iis与apache共用80端口方法集
    如何制作wordpress模板,WordPress模板技术手册
    zt 给博墅加个clocklink时钟
    MSSQL数据库:存储过程实例学习(1)从两个表中取出头两行,然后合并到一个表中
    ecmall 用户后台店铺条幅 在firefox7 以上无法预览的问题
    .NET MVC 实现动态换版
    一道嚼烂的面试题
    [1].gSOAP简介
  • 原文地址:https://www.cnblogs.com/Zhaowh/p/2915640.html
Copyright © 2011-2022 走看看