【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序

摘要:
就像许多工具(反光镜等)一样,可以去除糖衣。NET语法,我们可以使用许多工具来分析的性能。NET程序,如前一篇博客DebugLZQ中介绍的vs性能分析工具,尽管windag也提供图形界面操作。

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

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

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

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

CLR Profiler简介

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

ViewDescription
Histogram Allocated TypesGives 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 TypesDisplays the objects that the garbage collector has moved because they have survived a garbage collection.
Objects By AddressProvides a picture of what is on the managed heap at a given time.
Histogram By AgeAllows you to see the lifetime of the objects on the managed heap.
Allocation GraphGraphically 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 GraphThese four views are very similar. They allow you to see which methods pulled in which assemblies, functions, modules, or classes.
Heap GraphShows you all of the objects in the managed heap, along with their connections.
Call GraphLets 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 LineDisplays 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 ViewProvides 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程序的运行界面如下: 

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第1张

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

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第2张

 报告统计界面如下:

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第3张

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

Allocation Graph:用图表显示堆栈的分配情况【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第4张

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

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第5张

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

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

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第6张

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

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第7张

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

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第8张

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第9张

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

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第10张

 GC Handle: 统计GC句柄数

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第11张

具体细节如下:

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第12张

【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序第13张

就介绍到这里吧。

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

【希望对你有帮助~请点击下面的“绿色通道”---“关注DebugLZQ”,共同交流进步~】

免责声明:文章转载自《【.NET程序性能分析下篇】使用CLR Profiler分析.NET程序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇odoo javascriptoracle监听服务开启下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

iphone(ios)不同设备的内存和游戏不闪退峰值

ios内存限制 不同内存的苹果机型上(1G,2G,3G,4G…),游戏内存的峰值一般最高多少能保证不闪退? 一般来讲最保险的就是不超过机器总内存的50%,具体每个机型的内存限制在列出在下面。 原贴:《ios app maximum memory budget》 注意事项:查看当前app占用多少内存,通过OS的API来获取,而不要通过引擎自己的API来获取。...

学习如何高效率编写单片机代码,优化程序设计

1、 使用尽量小的数据类型 能用unsiged就不用signed;能用char就不用int;能不用floating就不用;能用位操作不用算数。 2、使用自加、自减指令 通常使用自加、自减指令和复合赋值表达式(如a-=1 及a+=1 等)都能够生成高质量的程序代码,编译器通常都能够生成inc 和dec 之类的指令,而使用a=a+1 或a=a-1 之类的指令,...

buuctf-re (持续更新)

buuctf 1.easyre 查壳: 没有壳,且为64位程序 分析: 使用ida64分析该文件发现,只是简单的判断我们的输入相等则输出flag。 #flag{this_Is_a_EaSyRe} 2.reverse1 查壳: 无壳,64位程序 分析: 使用ida64分析 输入的Str1字符串与Str2字符串比较,若相等则为flag。而Str2在...

「视频直播技术详解」系列之七:直播云 SDK 性能测试模型

​关于直播的技术文章不少,成体系的不多。我们将用七篇文章,更系统化地介绍当下大热的视频直播各环节的关键技术,帮助视频直播创业者们更全面、深入地了解视频直播技术,更好地技术选型。 本系列文章大纲如下: (一)采集 (二)处理 (三)编码和封装 (四)推流和传输 (五)延迟优化 (六)现代播放器原理 (七)SDK 性能测试模型 本篇是《视频直播技术详解》系列的...

String中intern方法的作用

前言 读完这篇文章你可以了解,String对象在虚拟机内存中的存放,intern的作用,这么多String对象的创建到底有什么区别,String 创建的对象有几个!! 正题 先科普几个知识点1.常量池存放于方法区中 2.jdk1.6 方法区放在永久代(java堆的一部分),jdk1.7 特别将字符串常量池移动到了的堆内存中(使用参数-XX:PermSize...

像素迷踪,当Unity的Frame Debugger力不从心时

http://www.manew.com/thread-92382-1-1.html 从版本5开始,Unity包含了一个全新的可视化帧调试工具,Frame Debugger。该工具能帮你解决很多图形方面的问题,Z-fighting,GPU状态不正常,渲染队列错误、混合操作错误,过多的draw call,效率低下等等。相比游戏视图中的状态列表,它提供了更加...