您的位置:首页 > 其它

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

2011-08-28 22:19 465 查看
http://msdn.microsoft.com/zh-cn/library/ms979205

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

注意:CLR Profiler最新版本:http://www.microsoft.com/download/en/details.aspx?id=16273

Identifying Common Garbage Collection Issues

可以使用CLR Profiler.exe来识别和隔离GC相关的内存问题,包括内存消耗相关的内容:

过多的分配。 Excessive Allocations

未知的分配。 Unknown Allocations

内存泄漏。 memory leaks.

也包含和GC相关的内容:

过多的收集。 Excessive Collections

长生命周期对象。 long-lived objects

每次GC操作消耗的时间百分比。 percentage of time spent performing garbage collection

注意:For more detailed information about using CLR Profiler to solve common problems related to garbage collection, see "Common Garbage Collection Problems and How They are Reflected In These Views" in CLRProfiler.doc, which is located in the installation folder of CLR Profiler.

Identifying Where Your Application Allocates Memory

当你和内存消耗的问题打交道的时候,最好知道你的应用程序在那里分配内存。

To identify where your application allocates memory, follow these steps:

Run CLR Profiler on the sample application. 在CLR Profiler上运行示例程序。

Analyze allocated memory types. 分析分配的内存类型。

Determine who is allocating the memory. 确定是谁在分配内存。

Evaluate what you can do to reduce the allocations. 评估你你能够采取的降低分配的措施。

Step 1. Run CLR Profiler on the Sample Application
Step 2. Analyze Allocated Memory Types
在View菜单上,点击[b]Histogram Allocated Types
. 显示如下图所示:[/b]




这张图显示在Application的生命周期中被分配的对象。在这个示例中,将近2G的对象被分配,大多数都是String. 原因是你使用示例中的字符串的连接方式,.net framework每次都分配一个新的长字符串,并且将旧的字符串复制过来。

Use the Histogram Allocated Types view to watch for objects that are allocated in the large object heap (those objects larger than 85 KB). You can select specific bar graphs in the left or right pane, and then right-click to see who allocated the memory. This view gives you a high-level view of the objects that are being allocated during the lifetime of your application.

Step 3. Determine Who is Allocating the Memory

点击Allocation Graph,你可以在图1中字符串区域点击右键,查看 show who allocated, 这个操作显示了被选定的分配的详细情况而不是所有的分配情况,如下图所示:





在这个示例中,你可以看到几乎所有的内存都是被String.Contact方法分配的。

Allocation Graph可以为你提供以下信息:

查看每个方法的分配成本

分析你不期望的分配

查看某个函数是否存在过多的分配

对比完成同样工作的不同方法

Step 4. Evaluate What You Can Do to Reduce the Allocations

现在你知道你的应用程序内存主要消耗在哪里了,可以采取措施降低内存的消耗了。在这个示例中,可以采用StringBuilder而不是String Concatenation.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐