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 }
复制代码

参考资料







本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/p/visual_studio_2013_new_feature_memory_dump_analysis.html,如需转载请自行联系原作者
目录
相关文章
|
8月前
|
C++ Windows
Visual Studio 2019 实现并行编译
使用 Visual Studio 2019 实现并行编译
144 0
Visual Studio 2019 实现并行编译
|
7月前
ABAP Memory Inspector 的功能简介
ABAP Memory Inspector 的功能简介
52 0
|
10月前
|
C++ Windows
Visual Studio 安装检测内存工具-Visual Leak Detetctor。(适用于VS2013、VS2015、VS2017、VS2019、VS2022版本)
Visual Studio 安装检测内存工具-Visual Leak Detetctor。(适用于VS2013、VS2015、VS2017、VS2019、VS2022版本)
265 0
|
11月前
|
安全 编译器 C++
Visual Studio 2022 版本 17.4 预览版 3 中对c++编译时优化的内容你都知道吗
Visual Studio 2022 版本 17.4 预览版 3 中对c++编译时优化的内容你都知道吗
|
Java Android开发 开发者
Android Studio Profiler Memory (内存分析工具)的简单使用及问题分析
Android Studio Profiler Memory (内存分析工具)的简单使用及问题分析
2567 0
Android Studio Profiler Memory (内存分析工具)的简单使用及问题分析
|
XML 数据格式
一个ABAP程序,能够下载指定的note到本地
一个ABAP程序,能够下载指定的note到本地
150 0
|
Linux Windows
【调试】Windows夯机Memory Dump案例分析
我们已经看了不少Linux的core dump分析案例了,这次我们来看一个案例,其中利用到了Windows memory dump的分析技巧。Windows的memory dump基本原理几乎和Linux并无太大区别,如果是Crash - 内核崩溃类型的dump,分析思路几乎是完全一致的,当然难度主要在于Windows系统封闭性,即无法提供私有符号和源码,所以多需要一些汇编层面的理解。
3148 0

热门文章

最新文章