.net 垃圾回收学习[How To: Use CLR Profiler][翻译学习]【2】
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:
Step 1. Run CLR Profiler on the Sample Application
Step 2. Analyze Allocated Memory Types?
???? 在View菜單上,點擊Histogram Allocated Types. 顯示如下圖所示:
這張圖顯示在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.
轉載于:https://www.cnblogs.com/sunshinefly128/archive/2011/08/28/2156486.html
總結
以上是生活随笔為你收集整理的.net 垃圾回收学习[How To: Use CLR Profiler][翻译学习]【2】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 3831
- 下一篇: Servlet编程API