代码改变世界

.net 垃圾回收学习[How To: Use CLR Profiler][翻译&&学习]

2011-08-28 20:52  一一九九  阅读(390)  评论(0编辑  收藏  举报

http://msdn.microsoft.com/zh-cn/library/ms979205

注意:内容可能已经过期了。

Improving .NET Application Performance and Scalability
J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman
Microsoft Corporation
May 2004
Contents

Overview
Downloading CLR Profiler
What You Must Know
Profiling Applications
Profiling ASP.NET Applications
Identifying Common Garbage Collection Issues
Identifying Where Your Application Allocates Memory
Analyzing Your Application's Allocation Profile
Sample: ProfilerSample1
Sample: ProfilerSample2
Additional Resources

Overview


采用CLR Profiler可以查看一个进程的Heap的情况,观察研究GC的行为,使用CLR Profiler提供的不同试图,可以获取应用程序的执行情况,内存分配和内存消耗情况。

CLR Profiler并不是分析问题的一个起点,更确切的说,CLR Profiler会帮助你识别和隔离有问题的代码,跟踪内存泄漏的情况。通过CLR Profiler,你可以找到那些分配了过多的内存,导致较多的垃圾回收,持有内存时间过长的代码。

注意:CLR Profiler是一个侵入式的应用程序,会导致你的程序比正常慢10倍到100倍。注意不要在发布环境中使用。

Downloading CLR Profiler


From:http://download.microsoft.com/download/4/4/2/442d67c7-a1c1-4884-9715-803a7b485b82/clr%20profiler.exe

What You Must Know


CLR Profiler的主要功能是向你提供你的应用程序和托管堆以及垃圾收集器交互的信息。通过CLR Profiler你可以得到如下的信息:

  • 谁在托管堆上分配了什么对象。who allocates what on the managed heap.
  • 哪个对象在托管堆上存活了。which objects survive on the managed heap.
  • 谁持有了对象。 who is holding on to objects.
  • GC 在你的程序的生命周期中做了什么事情。 what the garbage collector does over the lifetime of your application.

CLR Profiler的结果存储在日志文件中,你可以在CLR Profiler中打开,查看相关的视图。下表示几个最有用的视图。

Table1:CLR Profiler Views

视图 描述
Histogram Allocated Types(分配类型的历史) 呈现在你的应用程序在生命周期中分配哪些类型的对象。这个视图也展现了那些在Large Object Heap(Object Larger than 85kb)上的对象。通过点击图片的部分你能看到那些方法分配了这些对象。
Histogram Relocated types(重新定位类型的历史) 呈现了由于在GC过程中存活下来而被GC移动的对象。
Objects By Address 呈现在给定的时间点什么对象在托管堆上。
Histogram by Age (代际历史图) 查看托管推上的对象的生命周期
Allocation Graph 呈现了对象被分配的调用堆栈,可以使用该试图查看
×某个方法分配的代价
×隔离你不期望的分配过程
×查看某个方法是否分配了过多的对象。(进行了过多的分配操作)
Assembly, Module, Function, and class graph 这四个视图很类似。 他们允许你查看方法在那个程序集、函数、,模块或者类中。
Heap Graph 显示在托管堆上的所有对象,以及他们的关系
Call Graph 呈现那个方法调用了那个方法,以及调用的频次。
你可以通过该图推测类库调用的代价,决定调用的频次,以及哪些方法应该给调用(一句话,避免重复的多余的方法调用)
Time line 显示了GC在应用程序中的生命周期做了啥。
×观察GC的行为
×观察GC在3 Generation上分别调用了多次此,调用的频率
×观察哪些对象在GC之后仍然存活,并且被提升到更到的代中。
你可以选择时间点或者时间段来看在这一段时间内谁分配了这些内存
Call tree View 提供了一个基于文本的,按照时间顺序的,层级的应用程序执行视图。
×查看什么类型被分配以及他们的大小。
×方法调用导致了哪些程序集被调用
×分析finaizer的使用情况,包括finalizer的执行数目
×识别那些没有实现Close或者Dispose的方法,这种地方一般都是导致瓶颈的地方
*分析你不没有意料的内存分配。

Profiling Applications

Creating Sample Applications for Profiling


完整的源代码见页尾:

To create sample console applications for profiling


  1. 创建一个名为ProfilerSample的文件夹。
  2. 将页尾的ProfilerSample1, ProfilerSample2分别Copy到ProfilerSample的两个文件中。
  3. 编译文件
    csc /t:exe /out:ProfilerSample1.exe ProfilerSample1.cs
    csc /t:exe /out:ProfilerSample2.exe ProfilerSample2.cs
     

Using CLR Profiler to Profiler the Application


本次优化ProfilerSample.exe

To use CLR Profiler to profile the application
  • 启动CLR Profiler(CLRProfiler.exe)
  • 确保下述的选项被选中
    • profiling active
    • Allocations
    • Calls
  • 点击启动程序
  • 加载ProfilerSample.exe应用程序
  • 和程序进行必要的交互(运行程序),然后关闭程序。

Profiling ASP.NET Applications


To profile an ASP.NET application

  • 启动CLR Profiler
  • 确保以下选项被选中
    • Profiling Active
    • Allocations
    • Calls
  • 在菜单中,选择优化Asp.net应用程序。CLRProfiler 会关闭IIS,添加一些Profiling必须的环境变量,重新启动IIS。CLR Profiler会提示你加载Asp.net应用程序然后等待Asp.net进程启动。
  • 使用IE查看你要优化的Asp.net的应用程序
  • 当你结束运行的时候,你可以点击CLR Profiler主窗口的Kill Asp.net。 CLR会关闭IIS,恢复环境变量,并且重启IIS。注意:Sometimes the current version of the tool does not respond to the page load in Step 4. If this problem occurs, try changing the ASP.NET process identity to SYSTEM in the <ProcessModel> element in Machine.config. After you finish profiling your application, be certain that you change the application's identity back to machine.