通过上一篇文章一些截图,相信朋友们已经基本知道了Profiler的使用方法,现在,如何程序中的方法很多,例如2000个,那么通过上面的方法来查看就相当的痛苦了。怎么办呢?
一个比较不错的方法就是将信息导出成为报表的信息,然后在excel中进行分析。操作方法如下:

一旦上面的按钮点击之后,所有的调用树就会被显示出来,还没有将信息保存为csv格式,如下:

保存为了csv之后,我们通过excel打开,就可以进行分析了,如下:

除了采用上面的方法使用Profiler之外,我们还可以在代码中使用Profiler的功能。如果我们知道了哪些方法需要跟踪,我们可以在代码中开启Profiler。
首先,我们要在自己的项目中引用“CLRProfilerControl.dll”,然后可以写如下的代码:

这样写完之后呢,我们就可以运行profiler来启动我们的程序。这里要注意,我们必须禁用“profile active”,因为我们已经运行我们的代码运行profiler的检查跟踪功能,如下:

此时,如果我们查看“histogram”,就会发现数据非常的少,如下:

如果我们查看“allocation graph(内存分配图)”,此时,我们就会看到一个非常干净的界面了。和之前一样,我们还是要点击“everything”来看所有的方法的占用的内存,如下:

到这里,Profiler的使用就介绍完了,我们总结一下吧:
-
- Profiler可以用找出类,方法以及程序集占用内存的情况。
- 不能用在已经运行的上线环境中。
- 我们可以使用histogram查看内存的分配情况,通过call graph查看每个方法占用的内存。
- 我们可以通过代码的方式更加有针对性的对某些方法进行查看。