【.NET程序性能分析--下篇】使用CLR Profiler分析.NET程序
<div id="cnblogs_post_body"> 就像剥去.NET语法糖衣的工具(Reflector等)很多一样,我们可以用来分析.NET程序性能的工具有很多,如前面一片博文DebugLZQ给大家介绍的vs自带的性能分析工具,除此之外常用的还有还有clr profiler、Windbg等。vs自带的性能分析可以很快的找到瓶颈代码,而且支持多线程。
Windbg就不多说了,Windows平台下强大的用户态和内核态调试工具!虽然windbg也提供图形界面操作,但它最强大的地方还是有着强大的调试命令,用起来比较费劲。
这里主要要说的是CLR Profile了,他检测结果最为详细,不过由于检测托管堆分配和垃圾回收会影响应用程序的运行速度,因此无法得之时间上的性能测试。
CLR Profiler简介
CLR Profiler 是用来观察托管堆内存分配和研究垃圾回收行为的一种工具。使用该工具中不同的视图,你能获得关于你运用程序的执行、内存的分配和消耗等有用信息。CLR Profiler分析的结果存放在日志文件中,常用的几种视图如下:
ViewDescriptionHistogram 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.
下面还是以前面给出的代码为例,来介绍各种功能。代码如下:
<div class="cnblogs_code">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(); } }}
页:
[1]