鸿 网 互 联 www.68idc.cn

Windows Phone 8 显示当前项目的使用内存,最大峰值,最大内存上限

来源:互联网 作者:佚名 时间:2013-12-23 15:43
public static class MemoryDiagnosticsHelper { ; static Popup popup; static TextBlock currentMemoryKB; static TextBlock currentMemoryMB; static TextBlock currentLumitMemoryMB; static DispatcherTimer timer; static bool forceGc; const long MA

public static class MemoryDiagnosticsHelper { ; 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 > recentCheckpoints; // 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> )] forceGc) { isStart = true; ); MemoryDiagnosticsHelper.forceGc = forceGc; recentCheckpoints = >(); StartTimer(timespan); ShowPopup(); } /// <summary> /// Stops the timer and hides the counter /// </summary> )] 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> )] text) { ; 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> > RecentCheckpoints { get { ; 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) ); } /// <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) ); } /// <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) ); } /// <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) ); } private static void ShowPopup() { popup = new Popup(); ] - 2; ]; ] }; //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); .White) }; currentMemoryKB = .Red) }; currentLumitMemoryMB = .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; } timespan) { timer = new DispatcherTimer(); timer.Interval = timespan; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } e) { .Collect(); UpdateCurrentMemoryUsage(); UpdatePeakMemoryUsage(); } private static void UpdatePeakMemoryUsage() { ; 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 = ; currentMemoryMB.Text = ; currentLumitMemoryMB.Text = ; } catch { } //int safetyBand = GetSafetyBand(mem); //if (safetyBand != lastSafetyBand) //{ // currentMemoryKB.Foreground = GetBrushForSafetyBand(safetyBand); // lastSafetyBand = safetyBand; //} } safetyBand) { .Red); //switch (safetyBand) //{ // case 0: // return new SolidColorBrush(Colors.Green); // case 1: // return new SolidColorBrush(Colors.Orange); // default: // return new SolidColorBrush(Colors.Red); //} } mem) { )MAX_MEMORY; 0; 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> memoryUsage) { Text = text; MemoryUsage = memoryUsage; } /// <summary> /// The text associated with this checkpoint /// </summary> ; } /// <summary> /// The memory usage at the time of the checkpoint /// </summary> ; } } 调用方式:

网友评论
<