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 ...
随机推荐
- 0015 Java学习笔记-集合-TreeMap集合
主要的方法 构造方法: TreeMap(); TreeMap(Comparator<?super K> comparator); TreeMap(Map<? extends K,? ...
- windows环境下无法引用全局安装的模块问题
问题 在node项目中,往往需要安装一些依赖的包,通常我们采取全局安装的方式,来减少一些包重复安装带来的烦恼. 但是全局安装后出现无法使用的情况,可能是你NODE_PATH没有设置或者不正确造成的. ...
- [原]openstack-kilo--issue(六):Authorization Failed: The resource could not be found. (HTTP 404)
=======1.问题点:====== 在安装调试openstack-kilo版本的时候,使用keystone endpoint-list的时候出现了问题. 如下: [root@controller ...
- 金士顿U盘,群联PS2251-60主控,量产还原教程
还原成一个可移动磁盘教程,只是在"分区设置"中将 "模式=21" 改为 "模式=3" 即可. 1. 打开:"MPALL_F1_90 ...
- 一致性算法Paxos详解
分布式系统除了能提升整个系统的性能外还有一个重要的特性就是提高系统的可靠性,可靠性指的是当分布式系统中一台或N台机器宕掉后都不会导致系统不可用,分布式系统是state machine replicat ...
- Mysql数据库笔记
出错记录: 1.mysql服务启动不了,进程意外终止 1067 错误显示:can not connect to mysql server on local hosts(1061) 解决方法 ...
- 天朝使用GAE入门指南
0. 引言 Across the Great Wall, we can reach every corner in the world. 洒家最近玩了几下 Google App Engine.由于众所 ...
- Golang tips
1.go test 测试单个函数 go test -v -test.run Test* 2.
- 第63课 C语言异常处理
1. 异常的概念 (1)程序在运行过程中可能产生异常 (2)异常(Exception)与Bug的区别 ①异常是程序运行时可预料的执行分支 ②Bug是程序是的错误,是不被预期的运行方式 2. 异常和Bu ...
- 第14章 位图和位块传输_14.4 GDI位图对象(3)
14.4.10 非矩形的位图图像 (1)“掩码”位图——单色位图,要显示的像素对应的掩码置1,不显示置0(2)光栅操作(点这里,见此文分析) (3)MaskBlt函数 ①MaskBlt(hdcDest ...