使用CLR Profiler分析.NET程序

使用CLR Profiler分析.NET程序

就像剝去.NET語法糖衣的工具(Reflector等)不少同樣,咱們能夠用來分析.NET程序性能的工具備不少,如前面一片博文DebugLZQ給你們介紹的vs自帶的性能分析工具,除此以外經常使用的還有還有clr profiler、Windbg等。算法

  vs自帶的性能分析能夠很快的找到瓶頸代碼,並且支持多線程。多線程

  Windbg就很少說了,Windows平臺下強大的用戶態和內核態調試工具!雖然windbg也提供圖形界面操做,但它最強大的地方仍是有着強大的調試命令,用起來比較費勁。app

  這裏主要要說的是CLR Profile了,他檢測結果最爲詳細,不過因爲檢測託管堆分配和垃圾回收會影響應用程序的運行速度,所以沒法得之時間上的性能測試。ide

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.ui

-View possible excessive allocations by a method.this

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.spa

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。

相關文章
相關標籤/搜索