1. 程式人生 > 實用技巧 >提高.net程式效能和穩定性-CLR Profile(轉發)

提高.net程式效能和穩定性-CLR Profile(轉發)

原文:

https://blog.csdn.net/kntao/article/details/7077804

CLR Profile能夠看到應用程式的記憶體堆疊情況並且能夠查詢垃圾回收機制的行為。利用CLR Profile可以確定你的程式碼哪兒分配了太多記憶體,從而導致垃圾回收機制的執行,哪些程式碼長時間的佔有記憶體。不過CLR Profile不適合在生產環境下使用,因為如果用它,會使你的應用程式的效能下降10倍甚至100倍。

請從http://download.microsoft.com/download/4/4/2/442d67c7-a1c1-4884-9715-803a7b485b82/clr%20profiler.exe下載CLR Profile

CLR Profile 可以做:

  • 檢視託管堆上的物件
  • 檢視託管堆中存活的物件
  • 誰引用了託管堆上的物件
  • 垃圾回收機制在整個應用程式的生命週期內都做了什麼
ViewDescription
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 whereCloseorDisposehas not been implemented or called, thereby causing a bottleneck.

-Analyze allocations that you were not expecting.

啟動CLR Profile

選擇 Start Applcation 選擇exe檔案,然後等待應用程式執行完畢,然後就可以看到 我們選擇exe檔案的程式碼為:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "";
 
            DateTime begin = DateTime.Now;
            for (int i = 0; i < 10000; ++i)
                str += i;
            DateTime end = DateTime.Now;
 
            Console.WriteLine(begin - end);
        }
    }
}

  

點選Histogram可以看到記憶體分配情況和物件構造情況,從下圖可以看出string型別分配了300多M,30945 個例項物件。

點選Allocation Graph 可以分析出,可以檢視誰分配了物件佔用了記憶體

分析另一個應用程式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
 
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int start = Environment.TickCount;
            for (int i = 0; i < 100 * 1000; i++)
            {
                Brush b = new SolidBrush(Color.Black);    // Brush has a finalizer
                string s = new string(' ', i % 37);
 
                // Do something with the brush and the string.
                // For example, draw the string with this brush - omitted...
            }
            Console.WriteLine("Program ran for {0} seconds",
                              0.001 * (Environment.TickCount - start));
        }
    }
}

  

這段程式碼分配了100000個SolidBrush和一些string,導致了總共分配了大約9M記憶體。通過以下(第一幅是總得,第二副是回收後的)兩圖比較可以看出最後剩餘的記憶體很大部分是SolidBrush,大部分String物件都被回收。也就是說存活的物件都被提高到更高的代。

通過點選view-》Objects by Address可以看到物件佔用的記憶體空間。

通過點選view-》timeline 可以看到GC的行為

通過點選view->Call tree ,可以看到finalizers被呼叫的情況,

參考 :http://msdn.microsoft.com/en-us/library/ff650691.aspx