Windows Phone 8 显示当前项目的使用内存,最大峰值,最大内存上限
public static class MemoryDiagnosticsHelper
{
public static bool isStart = false;
static Popup popup;
static TextBlock currentMemoryKB;
static TextBlock currentMemoryMB;
static TextBlock currentLumitMemoryMB;
static DispatcherTimer timer;
static bool forceGc;
const long MAX_MEMORY = 90 * 1024 * 1024; // 90MB, per marketplace
static int lastSafetyBand = -1; // to avoid needless changes of colour const long MAX_CHECKPOINTS = 10; // adjust as needed
static Queue<MemoryCheckpoint> recentCheckpoints; static bool alreadyFailedPeak = false; // to avoid endless Asserts /// <summary>
/// Starts the memory diagnostic timer and shows the counter
/// </summary>
/// <param name="timespan">The timespan between counter updates</param>
/// <param name="forceGc">Whether or not to force a GC before collecting memory stats</param>
[Conditional("DEBUG")]
public static void Start(TimeSpan timespan, bool forceGc)
{
isStart = true;
if (timer != null) throw new InvalidOperationException("Diagnostics already running"); MemoryDiagnosticsHelper.forceGc = forceGc;
recentCheckpoints = new Queue<MemoryCheckpoint>(); StartTimer(timespan);
ShowPopup();
} /// <summary>
/// Stops the timer and hides the counter
/// </summary>
[Conditional("DEBUG")]
public static void Stop()
{
isStart = false;
HidePopup();
StopTimer();
recentCheckpoints = null;
} /// <summary>
/// Add a checkpoint to the system to help diagnose failures. Ignored in retail mode
/// </summary>
/// <param name="text">Text to describe the most recent thing that happened</param>
[Conditional("DEBUG")]
public static void Checkpoint(string text)
{
if (recentCheckpoints == null) return;
if (recentCheckpoints.Count >= MAX_CHECKPOINTS - 1) recentCheckpoints.Dequeue();
recentCheckpoints.Enqueue(new MemoryCheckpoint(text, GetCurrentMemoryUsage()));
} /// <summary>
/// Recent checkpoints stored by the app; will always be empty in retail mode
/// </summary>
public static IEnumerable<MemoryCheckpoint> RecentCheckpoints
{
get
{
if (recentCheckpoints == null) yield break; foreach (MemoryCheckpoint checkpoint in recentCheckpoints) yield return checkpoint;
}
} /// <summary>
/// Gets the current memory usage, in bytes. Returns zero in non-debug mode
/// </summary>
/// <returns>Current usage</returns>
public static long GetCurrentMemoryUsage()
{
// don't use DeviceExtendedProperties for release builds (requires a capability)
return (long)DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"); } /// <summary>
/// Gets the current memory usage, in bytes. Returns zero in non-debug mode
/// </summary>
/// <returns>Current usage</returns>
public static long GetCurrentPeakMemoryUsage()
{
// don't use DeviceExtendedProperties for release builds (requires a capability)
return (long)DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage"); } /// <summary>
/// Gets the peak memory usage, in bytes. Returns zero in non-debug mode
/// </summary>
/// <returns>Peak memory usage</returns>
public static long GetPeakMemoryUsage()
{
// don't use DeviceExtendedProperties for release builds (requires a capability)
return (long)DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage");
} /// <summary>
/// Gets the peak memory usage, in bytes. Returns zero in non-debug mode
/// </summary>
/// <returns>Peak memory usage</returns>
public static long GetLumitMemoryUsage()
{
// don't use DeviceExtendedProperties for release builds (requires a capability)
return (long)DeviceExtendedProperties.GetValue("ApplicationWorkingSetLimit");
} private static void ShowPopup()
{
popup = new Popup();
double fontSize = (double)Application.Current.Resources["PhoneFontSizeSmall"] - 2;
Brush foreground = (Brush)Application.Current.Resources["PhoneForegroundBrush"];
StackPanel sp = new StackPanel { Orientation = Orientation.Horizontal, Background = (Brush)Application.Current.Resources["PhoneSemitransparentBrush"] };
//currentMemoryKB = new TextBlock { Text = "---", FontSize = fontSize, Foreground = new SolidColorBrush(Colors.Red)};
//peakMemoryBlock = new TextBlock { Text = "", FontSize = fontSize, Foreground = new SolidColorBrush(Colors.White), Margin = new Thickness(5, 0, 0, 0) };
//sp.Children.Add(new TextBlock { Text = " kb", FontSize = fontSize, Foreground = foreground }); //sp.Children.Add(currentMemoryKB); currentMemoryMB = new TextBlock { Text = "---", FontSize = fontSize, Foreground = new SolidColorBrush(Colors.White) };
currentMemoryKB = new TextBlock { Text = "---", Margin = new Thickness(10, 0, 0, 0), FontSize = fontSize, Foreground = new SolidColorBrush(Colors.Red) };
currentLumitMemoryMB = new TextBlock { Text = "---", Margin = new Thickness(10, 0, 0, 0), FontSize = fontSize, Foreground = new SolidColorBrush(Colors.Green) };
sp.Children.Add(currentMemoryMB);
sp.Children.Add(currentMemoryKB);
sp.Children.Add(currentLumitMemoryMB); sp.RenderTransform = new CompositeTransform { Rotation = 0, TranslateX = 150, TranslateY = 0, CenterX = 0, CenterY = 0 };
popup.Child = sp;
popup.IsOpen = true;
} private static void StartTimer(TimeSpan timespan)
{
timer = new DispatcherTimer();
timer.Interval = timespan;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
} static void timer_Tick(object sender, EventArgs e)
{
if (forceGc) GC.Collect(); UpdateCurrentMemoryUsage();
UpdatePeakMemoryUsage(); } private static void UpdatePeakMemoryUsage()
{
if (alreadyFailedPeak) return; long peak = GetPeakMemoryUsage();
//if (peak >= MAX_MEMORY)
//{
// alreadyFailedPeak = true;
// Checkpoint("*MEMORY USAGE FAIL*");
// if (Debugger.IsAttached) Debug.Assert(false, "Peak memory condition violated");
//}
} private static void UpdateCurrentMemoryUsage()
{
try
{
long mem = GetCurrentMemoryUsage();
long feng = GetCurrentPeakMemoryUsage();
long lumit = GetLumitMemoryUsage();
currentMemoryKB.Text = string.Format("{0:f}", feng / 1024.0 / 1024.0) + "MB";
currentMemoryMB.Text = string.Format("{0:f}", mem / 1024.0 / 1024.0) + "MB";
currentLumitMemoryMB.Text = string.Format("{0:f}", lumit / 1024.0 / 1024.0) + "MB";
}
catch { }
//int safetyBand = GetSafetyBand(mem);
//if (safetyBand != lastSafetyBand)
//{
// currentMemoryKB.Foreground = GetBrushForSafetyBand(safetyBand);
// lastSafetyBand = safetyBand;
//}
} private static Brush GetBrushForSafetyBand(int safetyBand)
{
return new SolidColorBrush(Colors.Red);
//switch (safetyBand)
//{
// case 0:
// return new SolidColorBrush(Colors.Green); // case 1:
// return new SolidColorBrush(Colors.Orange); // default:
// return new SolidColorBrush(Colors.Red);
//}
} private static int GetSafetyBand(long mem)
{
double percent = (double)mem / (double)MAX_MEMORY;
if (percent <= 0.75) return 0; if (percent <= 0.90) return 1; return 2;
} private static void StopTimer()
{
timer.Stop();
timer = null;
} private static void HidePopup()
{
popup.IsOpen = false;
popup = null;
}
} /// <summary>
/// Holds checkpoint information for diagnosing memory usage
/// </summary>
public class MemoryCheckpoint
{
/// <summary>
/// Creates a new instance
/// </summary>
/// <param name="text">Text for the checkpoint</param>
/// <param name="memoryUsage">Memory usage at the time of the checkpoint</param>
internal MemoryCheckpoint(string text, long memoryUsage)
{
Text = text;
MemoryUsage = memoryUsage;
} /// <summary>
/// The text associated with this checkpoint
/// </summary>
public string Text { get; private set; } /// <summary>
/// The memory usage at the time of the checkpoint
/// </summary>
public long MemoryUsage { get; private set; }
}
调用方式:
MemoryDiagnosticsHelper.Start(TimeSpan.FromMilliseconds(500), true);
Windows Phone 8 显示当前项目的使用内存,最大峰值,最大内存上限的更多相关文章
- Windows中使用TortoiseGit提交项目到GitLab配置
下文来给各位介绍Windows中使用TortoiseGit提交项目到GitLab配置过程,下在全部图片希望对各位带来方便面. Gitlab默认的配置推荐使用shell命令行与server端进行交互,作 ...
- Git安装配置和提交本地代码至Github,修改GitHub上显示的项目语言
1. 下载安装git Windows版Git下载地址: https://gitforwindows.org/ 安装没有特别要求可以一路Next即可,安装完成后可以看到: 2. 创建本地代码仓库 打开G ...
- 怎么将linux下的项目转换成windows的VS2010下的项目?
怎么将linux下的项目转换成windows的VS2010下的项目? 不显示删除回复 显示所有回复 显示星级回复 ...
- 【转】Windows中使用TortoiseGit提交项目到GitLab配置
转 原文地址 https://www.cnblogs.com/xiangwengao/p/4134492.html 下文来给各位介绍Windows中使用TortoiseGit提交项目到GitLa ...
- [ASP.NET MVC] 使用CLK.AspNet.Identity提供依权限显示选单项目的功能
[ASP.NET MVC] 使用CLK.AspNet.Identity提供依权限显示选单项目的功能 CLK.AspNet.Identity CLK.AspNet.Identity是一个基于ASP.NE ...
- Windows Server 2008 显示桌面图标
相信有朋友们有安装使用过windows 2008 server服务器,刚安装好的时候,桌面上只有一个回收站的图标,它没有像windows 7或windows 8一样可以直接通过右击鼠标的菜单来设置,要 ...
- C#使用Windows API 隐藏/显示 任务栏 (FindWindowEx, ShowWindow)
原文 C#使用Windows API 隐藏/显示 任务栏 (FindWindowEx, ShowWindow) 今天,有网友询问,如何显示和隐藏任务栏? 我这里,发布一下使用Windows API 显 ...
- windows cmd命令显示UTF8设置
windows cmd命令显示UTF8设置 在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况 ...
- Jenkins 为Jenkins添加Windows Slave远程执行python项目脚本
为Jenkins添加Windows Slave远程执行python项目脚本 by:授客 QQ:1033553122 测试环境 JAVA JDK 1.7.0_13 (jdk-7u13-windows ...
随机推荐
- 六轴加速度传感器MPU6050官方DMP库到瑞萨RL78/G13的移植
2015年的电赛已经结束了.赛前接到器件清单的时候,看到带防护圈的多旋翼飞行器赫然在列,又给了一个瑞萨RL78/G13的MCU,于是自然联想到13年的电赛,觉得多半是拿RL78/G13做四旋翼的主控, ...
- Ubuntu Kylin15下PHP环境的搭建(LAMP)
Ubuntu下的PHP开发环境架设 今天重新装了ubuntu那么就吧过程记录下. 打开终端,也就是命令提示符. 我们先来最小化组建安装,按照自己的需求一步一步装其他扩展.命令提示符输入如下命令: ...
- 聚合数据董铭彦:小程序开发的兴起将带火API数据交易
2016中关村大数据日活动近日在京举办,今年新进驻北京的聚合数据受邀参与,在13日举行的大数据交易专场论坛上,聚合数据副总裁董铭彦与参会嘉宾以"共筑数据交易产业生态,共享大数据时代红利&qu ...
- nodejs模块——fs模块
fs模块用于对系统文件及目录进行读写操作. 一.同步和异步 使用require('fs')载入fs模块,模块中所有方法都有同步和异步两种形式. 异步方法中回调函数的第一个参数总是留给异常参数(exce ...
- 睡觉问题早晚成为我顶头疼的问题。。。-PHP
hi 昨晚又作自己,睡不好整个人都不好... 1.PHP实现页面静态化 二.纯静态化 2.2 实现页面纯静态化的原理 --基本方式 file_put_contents()函数: 使用php内置缓存机制 ...
- Color国际青年公寓
Color国际青年公寓介绍.md-/Users/zjh/Documents html{font-family: sans-serif;-ms-text-size-adjust: 100%;-webki ...
- Java的cmd配置(也即Java的JDK配置及相关常用命令)——找不到或无法加载主类 的解决方法
Java的cmd配置(也即Java的JDK配置及相关常用命令) ——找不到或无法加载主类 的解决方法 这段时间一直纠结于cmd下Java无法编译运行的问题.主要问题描述如下: javac 命令可以正 ...
- URL和URI的区别和联系
URI:Universal Resource Identifier,通用资源标识符: URL:Uniform Resource Locator,统一资源定位符: 其中,URL ...
- WCF添加服务失败一则
原因是本机开发IIS没有安装HTTPS证书 将红色的字注释掉就好了! <services> <service behaviorConfiguration="basicSer ...
- Font Awesome使用指南
Font Awesome介绍 Font Awesome是一款很流行的字体图标工具.随着Bootstrap的流行而逐渐被人所认识,现在FontAwesome不仅仅可以在bt上使用,还可以应用在各种web ...