使用CLR Profiler分析.NET程序

就像剥去.NET语法糖衣的工具(Reflector等)很多一样,我们可以用来分析.NET程序性能的工具有很多,如前面一片博文DebugLZQ给大家介绍的vs自带的性能分析工具,除此之外常用的还有还有clr profiler、Windbg等。

  vs自带的性能分析可以很快的找到瓶颈代码,而且支持多线程。

  Windbg就不多说了,Windows平台下强大的用户态和内核态调试工具!虽然windbg也提供图形界面操作,但它最强大的地方还是有着强大的调试命令,用起来比较费劲。

  这里主要要说的是CLR Profile了,他检测结果最为详细,不过由于检测托管堆分配和垃圾回收会影响应用程序的运行速度,因此无法得之时间上的性能测试。

CLR Profiler简介

CLR Profiler 是用来观察托管堆内存分配和研究垃圾回收行为的一种工具。使用该工具中不同的视图,你能获得关于你运用程序的执行、内存的分配和消耗等有用信息。CLR Profiler分析的结果存放在日志文件中,常用的几种视图如下:

View Description
Histogram Allocated Types Gives you a high-level view of what object types are allocated (by allocation size) during the lifetime of your application. This view also shows those objects that are allocated in the large object heap (objects larger than 85 KB).

This view allows you to click parts of the graph so that you can see which methods allocated which objects.

Histogram Relocated Types Displays the objects that the garbage collector has moved because they have survived a garbage collection.
Objects By Address Provides a picture of what is on the managed heap at a given time.
Histogram By Age Allows you to see the lifetime of the objects on the managed heap.
Allocation Graph Graphically displays the call stack for how objects were allocated. You can use this view to:

-See the cost of each allocation by method.

-Isolate allocations that you were not expecting.

-View possible excessive allocations by a method.

Assembly, Module, Function, and Class Graph These four views are very similar. They allow you to see which methods pulled in which assemblies, functions, modules, or classes.
Heap Graph Shows you all of the objects in the managed heap, along with their connections.
Call Graph Lets you see which methods call which other methods and how frequently.

You can use this graph to get a feel for the cost of library calls and to determine how many calls are made to methods and which methods are called.

Time Line Displays what the garbage collector does over the lifetime of the application. Use this view to:

-Investigate the behavior of the garbage collector.

-Determine how many garbage collections occur at the three generations (Generation 0, 1, and 2) and how frequently they occur.

-Determine which objects survive garbage collection and are promoted to the next generation.

You can select time points or intervals and right-click to show who allocated memory in the interval.

Call Tree View Provides a text-based, chronological, hierarchical view of your application's execution. Use this view to:

-See what types are allocated and their size.

-See which assemblies are loaded as result of method calls.

-Analyze the use of finalizers, including the number of finalizers executed.

-Identify methods where Close or Dispose has not been implemented or called, thereby causing a bottleneck.

-Analyze allocations that you were not expecting.

下面还是以前面给出的代码为例,来介绍各种功能。代码如下:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace VS2010性能测试
{
class Program
{
static void Main(string[] args)
{
int start = Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
string s = "";
for (int j = 0; j <200; j++)
{
s += "Outer index = ";
s += i;
s += " Inner index = ";
s += j;
s += " ";
}
}
int middle = Environment.TickCount;
Console.WriteLine("Program part1 run for {0} seconds",0.001 * (middle - start));
//
for (int i = 0; i < 1000; i++)
{
StringBuilder s = new StringBuilder();
for (int j = 0; j <200; j++)
{
s.Append("Outer index = ");
s.Append(i);
s.Append("Inner index = ");
s.Append(j);
s.Append(" ");
}
}
int end = Environment.TickCount;
Console.WriteLine("Program part2 run for {0} seconds", 0.001 * (end - middle)); //
Console.ReadKey();
}
}
}
 

CLR Profiler程序的运行界面如下:

通过start application 选择需要运行的程序,可以选择是否跟踪内存分配和方法调用。当关闭应用程序(可以自动或手动),Profiler自动开始整理结果。分析结果存放在日志文件中,显示如下:

报告统计界面如下:

Heap statistics 堆栈统计信息:DebugLZQ的这个测试程序需要分配6.6GB的内存!你有想到过吗?

Allocation Graph:用图表显示堆栈的分配情况

Allocated bytes:应用程序整个启动周期内分配的对象。按照对象大小排列,不同的颜色代码不同的对象,在右侧会列出。以下图为例,红色的是String对象。

Relocated bytes:GC时被对象在托管堆中的位置被移动过的。不同的颜色代表不同的对象。

(简要介绍下GC过程,大概分两步:有具体算法判断哪些对象成为了垃圾(即根据根引用列表遍历列表引用所指向的对象,不能被遍历的对象);移动堆中的不为垃圾的对象)

Final Heap bytes:最终还在堆中的。颜色代表种类。

Garbage Collection Statistics :GC统计信息。总共进行了4501次0代垃圾回收!

Time视图如下;从图中可以清晰的看出各次回收时间和前后内存占用量(总共4501次)。

GC Handle: 统计GC句柄数

具体细节如下:

就介绍到这里吧。

更为详细的信息,请阅读CLR Profiler 108页的详细说明,这个文档就在你释放出来的文件的根目录下,名称是CLRProfiler.doc。

使用CLR Profiler分析.NET程序的更多相关文章

  1. Net Memory Profiler 分析.Net程序内存泄露

    Net Memory Profiler 分析.Net程序内存泄露 Haozes's Tech Space 人類的全部才能無非是時間和耐心的混合物 使用.Net Memory Profiler 分析.N ...

  2. How To: Use CLR Profiler

    (翻译)How To: Use CLR Profiler   第一次翻译对我而言比较长的E文,有很多不足之处,请见谅.(个人的习惯GC又做了名词又做了名词) 原文:http://msdn.micros ...

  3. 使用VS自带的工具分析.NET程序的性能

    (转自:http://www.cnblogs.com/DebugLZQ/archive/2012/07/10/2585245.html) 这篇博文给大家分享的是,如何使用VS自带的性能分析工具来分析我 ...

  4. CLR Profiler 性能分析工具 (转)

    原文地址:http://www.cnblogs.com/kevinlzf/archive/2010/11/12/1876066.html 下载地址:http://www.microsoft.com/e ...

  5. CLR Profiler 性能分析工具

    CLR Profiler 性能分析工具 CLR Profiler 性能分析工具 CLR Profiler 有两个版本,分别用于CLR1.1 和 CLR2.0,至于CLR4试了一些也可以,但不知道是否完 ...

  6. 使用CLR Profiler查看C#运行程序的内存占用情况

    http://blog.csdn.net/wy3552128/article/details/8158938 https://msdn.microsoft.com/en-us/library/ff65 ...

  7. Use CLR Profiler

    Use CLR Profiler 第一次翻译对我而言比较长的E文,有很多不足之处,请见谅.(个人的习惯GC又做了名词又做了名词) 原文:http://msdn.microsoft.com/en-us/ ...

  8. CLR Profiler

    检查c#代码内存泄露工具-CLR Profiler 大家都知道.net有一套自己的内存(垃圾)回收机制,除非有一些数据(方法)长期占有内存不随着垃圾回收功能而释放内存,这样就造成了我们经常说的内存泄露 ...

  9. 转:检查c#代码内存泄露工具-CLR Profiler工具使用

    大家都知道.net有一套自己的内存(垃圾)回收机制,除非有一些数据(方法)长期占有内存不随着垃圾回收功能而释放内存,这样就造成了我们经常说的内存泄露.内存持续增长得不到释放等问题导致APS.NET网站 ...

随机推荐

  1. .net EF 事物 订单流水号的生成 (二):观察者模式、事物、EF

    针对.net EF 事物 订单流水号的生成 (一)  的封装. 数据依然不变. using System; using System.Linq; using System.Transactions; ...

  2. HDU 4324 Triangle LOVE 拓扑排序

    Problem Description Recently, scientists find that there is love between any of two people. For exam ...

  3. SOD框架的数据容器,打造最适合DDD的ORM框架

    SOD框架的数据容器,打造最适合DDD的ORM框架 引言:DDD的困惑 最近,我看到园子里面有位朋友的一篇博客 <领域驱动设计系列(一):为何要领域驱动设计? >文章中有下面一段话,对DD ...

  4. mvc中动态给一个Model类的属性设置验证

    原文:mvc中动态给一个Model类的属性设置验证 在mvc中有自带的验证机制,比如如果某个字段的类型是数字或者日期,那么用户在输入汉字或者英文字符时,那么编译器会自动验证并提示用户格式不正确,不过这 ...

  5. Ejb in action(七)——message与JMS

    我们扩大MDBs学前,我们需要理解message(新闻)与JMS(Java Message Service)的概念. 我们在Java EE中谈论消息,实际上就是意味着实现一个松耦合的过程.系统组件之间 ...

  6. hibernate缓存机制和事务隔离机制

    一级缓存( Session缓存) }         一级缓存的管理 ◦          应用程序调用Session的save().update().saveOrUpdate().get()或loa ...

  7. 第4章2节《MonkeyRunner源码剖析》ADB协议及服务: ADB服务SERVICES.TXT翻译参考(原创)

    天地会珠海分舵注:本来这一系列是准备出一本书的,详情请见早前博文“寻求合作伙伴编写<深入理解 MonkeyRunner>书籍“.但因为诸多原因,没有如愿.所以这里把草稿分享出来,所以错误在 ...

  8. asp.net中TextBox里面实现回车触发指定事件

    原文:asp.net中TextBox里面实现回车触发指定事件 我在一个user_top用户控件里面做了个包括搜索的功能.然后再一个页面中添加这个用户控件.浏览时候在textbox里面输入搜索内容后.下 ...

  9. C#播放流媒体的几种方法

    原文:[转载]C#播放流媒体的几种方法 做视频开发要学的东西真多,不知道如何入门,乱打乱撞,慢慢摸索吧! 首先搭建Windows Meida Server ,方法很简单,试试就会.在这里需要声明的是, ...

  10. leetcode[67] Plus One

    题目:对一个用vector存的数字进行加1,然后返回加1后的值. 一次就在oj上通过了. 就是进位加上当前位如果大于9,那就当前位等于0: 随后进位还为1的话就是在数组前面插入一个1: class S ...