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 ...
随机推荐
- 烂泥:使KVM显示VM的IP地址及主机名
本文由秀依林枫提供友情赞助,首发于烂泥行天下. KVM虚拟化学习已经有一段时间了,现在虚拟化软件比较多,对比了下目前使用比较多的VMware Vsphere.发现在不进入VM系统的情况下,Vspher ...
- x01.os.11: IPC 路线图
学习的最好方法就是看代码,所以我们不妨跟着 IPC 的调用路线图,来学习学习 IPC. 从 x01.Lab.Download 下载代码后,首先进入 main.c 文件,在 TestA 中,有这么一句: ...
- proteus汉化
下载地址: http://files.cnblogs.com/files/xiaobo-Linux/proteus7%E6%B1%89%E5%8C%96.zip (别的版本也应该可以汉化) 将这ARE ...
- emacs 新手笔记(二) —— 分割窗格 (split window)
初极狭,才通人.复行数十步,豁然开朗.—— 陶渊明·桃花源记 ilocker:关注 Android 安全(新入行,0基础) QQ: 2597294287 使用 split-window-xxx 函数可 ...
- ASP.NET Web API 安全筛选器
原文:https://msdn.microsoft.com/zh-cn/magazine/dn781361.aspx 身份验证和授权是应用程序安全的基础.身份验证通过验证提供的凭据来确定用户身份,而授 ...
- 新手ui设计师必备——切图规范
图文并茂,浅显易懂. 使用markman标注. 资源链接: 图片来自http://www.ui.cn/detail/56347.html 本文作者starof,因知识本身在变化,作者也在不断学习成长, ...
- [转]教你一招 - 如何给nopcommerce增加新闻类别模块
本文转自:http://www.nopchina.net/post/nopchina-teach-newscategory.html nopcommerce的新闻模块一直都没有新闻类别,但是很多情况下 ...
- Java开发之JSP指令
一.page指令 page指令是最常用的指令,用来说明JSP页面的属性等.JSP指令的多个属性可以写在一个page指令里,也可以写在多个指令里.但需要注意的是,无论在哪个page指令里的属性,任何pa ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- java 22 - 12 多线程之解决线程安全问题的实现方式1
从上一章知道了多线程存在着线程安全问题,那么,如何解决线程安全问题呢? 导致出现问题的原因: A:是否是多线程环境 B:是否有共享数据 C:是否有多条语句操作共享数据 上一章的程序,上面那3条都具备, ...