Visual Studio 2013 新功能 Memory Dump 分析器

 

TechEd2013 发现新功能

12月5日和6日,在国家会议中心参加了微软的 TechEd2013 技术大会,了解了很多微软所提供的软件新功能和新技术。

在上面的图中描述了在 Visual Studio 2013 中提供了新的功能 .NET Memory Dump Analysis,在具体的 Visual Studio 2013 新功能介绍的 Session 看到了实际的演示。当时感觉这个新功能对开发人员太有帮助了,因为使用 WinDbg 进行内存泄漏等问题排查总是一种痛苦。如果能在 Visual Studio 中包含类似的功能就再好不过了。

准备尝鲜 .NET Memory Dump Analysis

回到家,自然是想使用 Visual Studio 2013 直接上手实际体验下。

在创造了一个 Dump 文件之后,使用 Visual Studio 2013 直接打开,

结果发现,在 Actions 处少一个 "Debug Managed Memory" 按钮。

它应该长这个样子才对:

Google 之后发现原来我使用的是 Visual Studio 2013 Premium 版本,而该功能只有在 Visual Studio 2013 Ultimate 版本中才支持。

公司的 MSDN 订阅选的是 Premium ,比较了下与 Ultimate 的区别,价格果真差的很大。

此时,我选择了安装个 Windows 7 SP1 虚拟机,并安装 Visual Studio 2013 Ultimate 的试用版。

遭遇 Visual Studio 2013 的 Known Issue

显然,安装上 Visual Studio 2013 Ultimate 之后,再打开 Dump 文件,就可以成功看到 Actions 中的 "Debug Managed Memory" 按钮。

点击该按钮,等待 Heap View 显示。

什么?白屏?一万只马飘过。

这可真是郁闷,折腾了半天都没反应。

于是只能问 Google 了,显然,新的功能暂时没什么人用,也搜不到什么有用的讨论。

终于,当搜索关键词修改为 “Visual Studio 2013 Debug Managed Memory is not work” 时,有用的信息出现了。

打开一看,这居然是一个 Visual Studio 2013 Known Issue,基本就是要求安装 IE10 或以上的版本才能使用此功能。

解决办法,当然就是安装新版 IE,立马下载安装 IE11。新装的虚拟机中的 Windows 7 默认的还是 IE8 。

使用 Dump 文件比较功能

这一次,成功看到了 Heap View。

在 Compare to 中选择一个之前的 Dump 文件作为对比,即可查看两个文件时间的内存变化。

Inclusive Size

在 Heap View 中,有一列为 Inclusive Size (Bytes)。

其包含所显示对象引用的所有对象内存占用的总和。下面的图标展示了内存占用大小的计算过程。

Object Type

Count

Size (Bytes)

Inclusive Size (Bytes)

Customer

1

100

400

Address

1

50

200

String

2

150

150

Byte[]

1

100

100

Address 对象的 Inclusive Size = Address (50 bytes) + String (50 bytes) + Byte[] (100 bytes) = 200 bytes

Customer 对象的 Inclusive Size = Address Inclusive Size (200 bytes) + String (100 bytes) + Customer (100 bytes) = 400 bytes

Inclusive Size Diff

在使用 Dump 比较功能后,显示的列中会出现 "Inclusive Size Diff",用来描述在两个 Dump 文件采集的时间点时,Inclusive Size 的变化。

View Setting

在 Dump 比较的界面中,我们可以看到 View Settings 选项。

  • Just My Code :顾名思义,仅显示用户代码。
  • Collapse Small Objects :隐藏小对象。

这里需要描述下,小对象隐藏后的 Inclusive Size 的计算方式。

实际上,会将被隐藏的小对象的大小累加到父对象上。

仍然使用上面的图例进行计算。

我们假设 String 和 Byte[] 被隐藏,在隐藏小对象后,

Object Type

Count

Size (Bytes)

Inclusive Size (Bytes)

Customer

1

200

400

Address

1

200

200

Paths To Root View

点击 Heap View 列表中的某一个 Object Type,在 Paths To Root 中会通过级联方式显示对象的引用关系。

这用于显示,是哪个对象引用导致该对象没有被垃圾回收。

References View

References View 用于显示,这个对象都引用了哪些对象。

测试用代码

 1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Threading;
5
6 namespace TestDebugMemoryDumpFile
7 {
8 class Program
9 {
10 static ConcurrentQueue<Tree> _leakedTrees = new ConcurrentQueue<Tree>();
11
12 static void Main(string[] args)
13 {
14 List<string> fruits = new List<string>() // 6 items
15 {
16 "Apple", "Orange", "Pear", "Banana", "Peach", "Hawthorn",
17 };
18
19 Random random = new Random();
20 while (true)
21 {
22 string fruitName = fruits[random.Next(0, 5)];
23 Tree fruitTree = new Tree(fruitName);
24 BuildFruitTree(fruitTree, 100); // 100M
25 _leakedTrees.Enqueue(fruitTree);
26
27 Console.WriteLine("Added [{0}] on [{1}]...",
28 fruitTree.Name,
29 DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.ffffff"));
30
31 Thread.Sleep(TimeSpan.FromSeconds(5));
32 }
33 }
34
35 private static void BuildFruitTree(Tree fruitTree, int leafCount)
36 {
37 Console.WriteLine("Building [{0}] on [{1}]...",
38 fruitTree.Name,
39 DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss.ffffff"));
40
41 for (int i = 0; i < leafCount; i++) // size M
42 {
43 Leaf<byte[]> leaf = new Leaf<byte[]>(Guid.NewGuid())
44 {
45 Content = CreateContentSizeOfOneMegabyte()
46 };
47 fruitTree.Leaves.Add(leaf);
48 }
49 }
50
51 private static byte[] CreateContentSizeOfOneMegabyte()
52 {
53 byte[] content = new byte[1024 * 1024]; // 1 M
54 for (int j = 0; j < content.Length; j++)
55 {
56 content[j] = 127;
57 }
58 return content;
59 }
60 }
61
62 internal class Tree
63 {
64 public Tree(string name)
65 {
66 Name = name;
67 Leaves = new List<Leaf<byte[]>>();
68 }
69
70 public string Name { get; private set; }
71 public List<Leaf<byte[]>> Leaves { get; private set; }
72 }
73
74 internal class Leaf<T>
75 {
76 public Leaf(Guid id)
77 {
78 Id = id;
79 }
80
81 public Guid Id { get; private set; }
82 public T Content { get; set; }
83 }
84 }

参考资料

Memory Dump 分析器的更多相关文章

  1. Visual Studio 2013 新功能 Memory Dump 分析器

    本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. TechEd2013 发现新功能 12月5日和6日,在国家会议中心参加了微软的 TechEd2013 ...

  2. Responder Pro new version could analyze Win10 memory dump

    My friend John acquired a memory dump from Windows 10, but he could analyze this memory dump with an ...

  3. qualcomm memory dump 抓取方法

    Memory dump是系统出现crash时常用的分析故障原因的方法,qualcomm 各子系统运行时,为方便debug,都会开辟ram log和debug variable用于保存各系统运行信息及健 ...

  4. How do I find what queries were executing in a SQL memory dump?-----stack

     https://blogs.msdn.microsoft.com/askjay/2010/10/03/how-do-i-find-what-queries-were-executing-in-a-s ...

  5. 利用 Memory Dump Diagnostic for Java (MDD4J) 分析内存管理问题

    利用 Memory Dump Diagnostic for Java (MDD4J) 分析内存管理问题(2) 启动和理解 MDD4J[size=1.0625]为了充分理解如何使用 MDD4J,您需要了 ...

  6. memory dump and CLR Inside Out

    MSDN Magazine: CLR Inside Out https://msdn.microsoft.com/en-us/magazine/cc501040.aspx CLR Inside Out ...

  7. Android memory dump

    1.读取指定pid和内存地址的字符: #include <stdlib.h> #include <stdio.h> #include <string.h> #inc ...

  8. .NET Memory Profiler 查看内存使用情况

    1 简介 .Net Memory Profiler(以下简称Profiler):专门针对于.NET程序,功能最全的内存分析工具,最大的特点是具有内存动态分析(Automatic Memory Anal ...

  9. SharePoint运行状况分析器有关磁盘空间不足的警告

    对于负责管理SharePoint内部部署安装的SharePoint管理员,SharePoint Health Analyzer是一款出色的工具.此功能不仅有助于解决服务器故障和服务失败的问题,还提供了 ...

随机推荐

  1. thinkphp学习笔记2—入口文件

    原文:thinkphp学习笔记2-入口文件 在thinkphp中有两个入口文件,一个是项目的入口文件,是index.php在主目录里面,还有一个是thinkphp框架的的入口文件,放在框架目录下面如: ...

  2. HDU 4901 The Romantic Hero(二维dp)

    题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候依照给的先后数序取数,后面的里面的全部的元素的下标一定比前面的大.问你有多上种放元素的方法能够使 ...

  3. 软体project(四)——一生

    软件生存周期是软件project中的一个重要概念,把整个生存周期划分为若干个阶段,是实现软件生产project化的重要步骤. 软件的生存周期一般划分为软件计划.软件开发和软件执行三个时期,例如以下图: ...

  4. Android Application.java以及它的作用

    What is Application Application和Activity,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application ...

  5. Atitit.异步编程 java .net php python js 对照

    Atitit.异步编程 java .net php python js 的比較 1. 1.异步任务,异步模式,  APM模式,,  EAP模式, TAP 1 1.1.       APM模式: Beg ...

  6. CSDN博客频道维护公告

    各位亲爱的用户:        为了给大家提供更稳定的使用环境,2014年4月23日23点至04月24日1点(本周四凌晨)博客频道server将进行维护,维护期间不能正常訪问.给大家带来不便,敬请广大 ...

  7. BS导出csv文件的通用方法(.net)

    最近把以前项目里用的导出文件的功能提取成了dll,通过读取Attribute来得到要导出的表头(没有支持多语言),使用时只要组织好要导出的数据,调用方法就好了,希望对大家有用. 使用时只需引用下载包里 ...

  8. Android Notification通知详细解释

    Android Notification通知具体解释  Notification: (一).简单介绍:         显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...

  9. Android开发----------- 手电筒改进版本号

    在之前的基础上 在 res 目录以下: 加入一个 drawable/local_me.xml localme_cml <selector xmlns:android="http://s ...

  10. 转载JQuery绑定鼠标粘贴事件工具类

    // 粘贴事件监控 $.fn.pasteEvents = function( delay ) { if (delay == undefined) delay = 10; return $(this). ...