CLR Profile能够看到应用程序的内存堆栈情况并且能够查询垃圾回收机制的行为。利用CLR Profile可以确定你的代码哪儿分配了太多内存,从而导致垃圾回收机制的执行,哪些代码长时间的占有内存。不过CLR Profile不适合在生产环境下使用,因为如果用它,会使你的应用程序的性能下降10倍甚至100倍。

请从http://download.microsoft.com/download/4/4/2/442d67c7-a1c1-4884-9715-803a7b485b82/clr%20profiler.exe下载CLR Profile

CLR Profile 可以做:

  • 查看托管堆上的对象
  • 查看托管堆中存活的对象
  • 谁引用了托管堆上的对象
  • 垃圾回收机制在整个应用程序的生命周期内都做了什么
View Description
Histogram Allocated Types Gives you a high-level view of what object types are allocated (by allocation size) during the lifetime of your application. This view also shows those objects that are allocated in the large object heap (objects larger than 85 KB).

This view allows you to click parts of the graph so that you can see which methods allocated which objects.

Histogram Relocated Types Displays the objects that the garbage collector has moved because they have survived a garbage collection.
Objects By Address Provides a picture of what is on the managed heap at a given time.
Histogram By Age Allows you to see the lifetime of the objects on the managed heap.
Allocation Graph Graphically displays the call stack for how objects were allocated. You can use this view to:

-See the cost of each allocation by method.

-Isolate allocations that you were not expecting.

-View possible excessive allocations by a method.

Assembly, Module, Function, and Class Graph These four views are very similar. They allow you to see which methods pulled in which assemblies, functions, modules, or classes.
Heap Graph Shows you all of the objects in the managed heap, along with their connections.
Call Graph Lets you see which methods call which other methods and how frequently.

You can use this graph to get a feel for the cost of library calls and to determine how many calls are made to methods and which methods are called.

Time Line Displays what the garbage collector does over the lifetime of the application. Use this view to:

-Investigate the behavior of the garbage collector.

-Determine how many garbage collections occur at the three generations (Generation 0, 1, and 2) and how frequently they occur.

-Determine which objects survive garbage collection and are promoted to the next generation.

You can select time points or intervals and right-click to show who allocated memory in the interval.

Call Tree View Provides a text-based, chronological, hierarchical view of your application's execution. Use this view to:

-See what types are allocated and their size.

-See which assemblies are loaded as result of method calls.

-Analyze the use of finalizers, including the number of finalizers executed.

-Identify methods where Close or Dispose has not been implemented or called, thereby causing a bottleneck.

-Analyze allocations that you were not expecting.

启动CLR Profile

选择 Start Applcation 选择exe文件,然后等待应用程序执行完毕,然后就可以看到
我们选择exe文件的代码为:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication2
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string str = "";
  12. DateTime begin = DateTime.Now;
  13. for (int i = 0; i < 10000; ++i)
  14. str += i;
  15. DateTime end = DateTime.Now;
  16. Console.WriteLine(begin - end);
  17. }
  18. }
  19. }

点击Histogram可以看到内存分配情况和对象构造情况,从下图可以看出string类型分配了300多M,30945 个实例对象。
点击 Allocation Graph 可以分析出,可以查看谁分配了对象占用了内存

分析另一个应用程序:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. namespace ConsoleApplication2
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int start = Environment.TickCount;
  13. for (int i = 0; i < 100 * 1000; i++)
  14. {
  15. Brush b = new SolidBrush(Color.Black);    // Brush has a finalizer
  16. string s = new string(' ', i % 37);
  17. // Do something with the brush and the string.
  18. // For example, draw the string with this brush - omitted...
  19. }
  20. Console.WriteLine("Program ran for {0} seconds",
  21. 0.001 * (Environment.TickCount - start));
  22. }
  23. }
  24. }

这段代码分配了100000个SolidBrush和一些string,导致了总共分配了大约9M内存。通过以下(第一幅是总得,第二副是回收后的)两图比较可以看出最后剩余的内存很大部分是SolidBrush,大部分String对象都被回收。也就是说存活的对象都被提高到更高的代。



通过点击view-》Objects by Address 可以看到对象占用的内存空间。
 
通过点击view-》timeline 可以看到GC的行为

通过点击view->Call tree ,可以看到finalizers被调用的情况,

提高.net程序性能和稳定性-CLR Profile的更多相关文章

  1. 一个用于每一天JavaScript示例-使用缓存计算(memoization)为了提高应用程序性能

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. 提高WPF程序性能的几条建议

    这篇博客将介绍一些提高WPF程序的建议(水平有限,如果建议有误,请指正.) 1. 加快WPF程序的启动速度: (1).减少需要显示的元素数量,去除不需要或者冗余的XAML元素代码. (2).使用UI虚 ...

  3. 用 Function.apply() 的参数数组化来提高 JavaScript程序性能

    我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...

  4. 使用BOOST BIND库提高C++程序性能

    Boost.Bind为函数和函数对象,值语义和指针提供语义了一致的语法.我们首先通过一些简单的例子来看看它的基本用法,之后我们会延伸到嵌套绑定以实现功能组合.理解bind用法的一个关键是理解占位符(p ...

  5. 提高ASP.NET应用程序性能的十大方法

    一.返回多个数据集 检查你的访问数据库的代码,看是否存在着要返回多次的请求.每次往返降低了你的应用程序的每秒能够响应请求的次数.通过在单个数据库请求中返回多个结果集,可以减少与数据库通信的时间,使你的 ...

  6. asp.net提高程序性能的技巧(一)

    [摘 要] 我只是提供我几个我认为有助于提高写高性能的asp.net应用程序的技巧,本文提到的提高asp.net性能的技巧只是一个起步,更多的信息请参考<Improving ASP.NET Pe ...

  7. 【翻译】七个习惯提高Python程序的性能

    原文链接:https://www.tutorialdocs.com/article/7-habits-to-improve-python-programs.html 掌握一些技巧,可尽量提高Pytho ...

  8. 如何提高NodeJS程序的运行的稳定性

    如何提高NodeJS程序运行的稳定性 我们常通过node app.js方式运行nodejs程序,但总有一些异常或错误导致程序运行停止退出.如何保证node程序的稳定运行? 下面是一些可以考虑的方案: ...

  9. 提高ASP.net性能的十种方法

    提高ASP.net性能的十种方法 2014-10-24  空城66  摘自 博客园  阅 67  转 1 转藏到我的图书馆   微信分享:   今天无意中看了一篇关于提高ASP.NET性能的文章,个人 ...

随机推荐

  1. python中在ubuntu中安装虚拟环境及环境配置

    python中在ubuntu中安装虚拟环境及环境配置 1.升级python包管理工具pip pip install --upgrade pip 备注:当你想升级一个包的时候 `pip install ...

  2. 【JavaScript】ReactJS基础

    初探React,将我们的View标签化 前言 我之前喜欢玩一款游戏:全民飞机大战,而且有点痴迷其中,如果你想站在游戏的第一阶梯,便需要不断的练技术练装备,但是腾讯的游戏一般而言是有点恶心的,他会不断的 ...

  3. vi 新建编辑文件时报错 E212 can’t open file for writing

    在vi修改防火墙配置时,不能够保存,报E212 can’t open file for writing错误. 网上大概给出了两种答案. 一是权限不够,可以用root权限事实,或者sudo 操作. 二是 ...

  4. Java 8 Stream 教程

    Java 8 Stream Tutorial 本文采用实例驱动的方式,对JAVA8的stream API进行一个深入的介绍.虽然JAVA8中的stream API与JAVA I/O中的InputStr ...

  5. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)如何修改标准驱动器编码器分辨率

    在某个轴的Enc上双击,可以修改Scaling Factor Numerator     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetao ...

  6. HTTP常用端口号与对应的服务说明

    常用端口号与对应的服务以及端口关闭 端口简介:本文介绍端口的概念,分类,以及如何关闭/开启一个端口 21端口:21端口主要用于FTP(File Transfer Protocol,文件传输协议)服务. ...

  7. vue项目中provide和inject的运用

    类型: provide:Object | () => Object inject:Array<string> | { [key: string]: string | Symbol | ...

  8. 极光推送sdk使用

    创建应用 进入极光控制台后,点击“创建应用”按钮,进入创建应用的界面. 填上你的应用程序的名称以及应用包名这二项就可以了, 最后点击最下方的 “创建我的应用”按钮,创建应用完毕.   创建应用   填 ...

  9. jQuery实现滚动栏一直处于最底部

    相信大家有时候在展示一些实时数据展示并且数据量非常大的时候,由于无法在同一页面看到最有效的数据,所以我们须要将滚动栏至于底部.以便我们看到最须要的数据和信息.这里非常明显的样例那拿windows的pi ...

  10. ES6 async 与 await 实战

    下面来说一说通过async和await方式来辅助请求和封装 首先我们定义一个类,定义一个async方法,才可以使用await class JForm extends React.Component { ...