.net 垃圾回收学习[How To: Use CLR Profiler][翻译学习]【2】
http://msdn.microsoft.com/zh-cn/library/ms979205
注意:內(nèi)容可能已經(jīng)過期了。
注意:CLR Profiler最新版本:http://www.microsoft.com/download/en/details.aspx?id=16273
Identifying Common Garbage Collection Issues
可以使用CLR Profiler.exe來識(shí)別和隔離GC相關(guān)的內(nèi)存問題,包括內(nèi)存消耗相關(guān)的內(nèi)容:
- 過多的分配。 Excessive Allocations
- 未知的分配。 Unknown Allocations
- 內(nèi)存泄漏。 memory leaks.
也包含和GC相關(guān)的內(nèi)容:
- 過多的收集。 Excessive Collections
- 長生命周期對(duì)象。 long-lived objects
- 每次GC操作消耗的時(shí)間百分比。 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
當(dāng)你和內(nèi)存消耗的問題打交道的時(shí)候,最好知道你的應(yīng)用程序在那里分配內(nèi)存。
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菜單上,點(diǎn)擊Histogram Allocated Types. 顯示如下圖所示:
這張圖顯示在Application的生命周期中被分配的對(duì)象。在這個(gè)示例中,將近2G的對(duì)象被分配,大多數(shù)都是String. 原因是你使用示例中的字符串的連接方式,.net framework每次都分配一個(gè)新的長字符串,并且將舊的字符串復(fù)制過來。
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
點(diǎn)擊Allocation Graph,你可以在圖1中字符串區(qū)域點(diǎn)擊右鍵,查看 show who allocated, 這個(gè)操作顯示了被選定的分配的詳細(xì)情況而不是所有的分配情況,如下圖所示:
在這個(gè)示例中,你可以看到幾乎所有的內(nèi)存都是被String.Contact方法分配的。
Allocation Graph可以為你提供以下信息:
- 查看每個(gè)方法的分配成本
- 分析你不期望的分配
- 查看某個(gè)函數(shù)是否存在過多的分配
- 對(duì)比完成同樣工作的不同方法
Step 4. Evaluate What You Can Do to Reduce the Allocations
現(xiàn)在你知道你的應(yīng)用程序內(nèi)存主要消耗在哪里了,可以采取措施降低內(nèi)存的消耗了。在這個(gè)示例中,可以采用StringBuilder而不是String Concatenation.
轉(zhuǎn)載于:https://www.cnblogs.com/sunshinefly128/archive/2011/08/28/2156486.html
總結(jié)
以上是生活随笔為你收集整理的.net 垃圾回收学习[How To: Use CLR Profiler][翻译学习]【2】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 3831
- 下一篇: c++ 返回 char*