相關閱讀 |
>>> 技術話題—商業文明的嶄新時代 >>> | 簡體 傳統 |
就像剝去.NET語法糖衣的工具(Reflector等)很多一樣,我們可以用來分析.NET程序性能的工具有很多,如前面一片博文DebugLZQ給大家介紹的vs自帶的性能分析工具,除此之外常用的還有還有clr profiler、Windbg等。
vs自帶的性能分析可以很快的找到瓶頸代碼,而且支持多線程。
Windbg就不多說了,Windows平臺下強大的用戶態和內核態調試工具!雖然windbg也提供圖形界面操作,但它最強大的地方還是有著強大的調試命令,用起來比較費勁。
這里主要要說的是CLR Profile了,他檢測結果最為詳細,不過由于檢測托管堆分配和垃圾回收會影響應用程序的運行速度,因此無法得之時間上的性能測試。
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。
網載 2013-07-22 22:50:05
稱謂:
内容: