DotMemory a series of tutorials (acquaintance dotmemory)

       Why write this dotmemory it, or because before the interview, the blood of abuse. Although read it twice before CLR VIA C #, but there is talk of CLR theory knowledge is to say, he did not go to the actual verification. When the interview, the interviewer asked a number of registers and stack memory cache, memory allocation and optimization as well as some of the more narrow question of gc. The hammer. So wanted by a number of tools to see how matter in the end, verify relevant knowledge Clr memory and so on. But eleven many ladders were all the walls, google impossible, the result of Baidu out, did not this knowledge, the only and some only a tutorial, the results of this tutorial written feel very take heart, compared to java-related content, whatever a recall is very detailed and written various books, really .net ecological far worse. So some angry, but not any way, after all, ecological or rely on those of us engaged in efforts .net slowly, so they are ready to combine my official documents should be translated English city of a tutorial. And next thought, his colleagues, and he wrote many powerful tools, it is estimated that dotmemory finish will write about some of the tools windbg and colleagues wrote it myself.

       In this tutorial, we will learn how to use and how to obtain dotMemory memory snapshot. In addition, we will briefly describe the user interface and the basic concepts of dotMemory. And this tutorial as your dotMemory the introductory tutorial.

        Basic terms 

      You may ask: "What is the memory snapshot, they do?" The so-called memory snapshots, it is that the program is running, memory usage and distribution of a certain moment, similar to the memory-related information this time took a photo . Snapshots are similar understanding, we can understand the analogy redis snapshot. When you use the DotMemory, you'll have some of these impression.

        From the memory point of view, the work of the application include objects continue to allocate memory for the new object, and release the application is no longer using the memory left. Object is allocated one by one in a so-called managed heap. Based on this, as a memory profiler We have two basic operations must be able to do:

        1) get a memory snapshot. Snapshots are managed heap real-time image. Each snapshot contains information about all objects when you click the Snapshot button, the application allocated in memory.

        2) to collect traffic information memory. Show your traffic memory allocation and release how much memory, for example, between two memory snapshots. This information is very valuable because it allows you to understand how application memory is dynamic.

       Of course, in this tutorial, you'll learn some other terminology. But now it was enough to understand the next steps in the tutorial. let's start!

      Sample Application 

    First, we need an application for analysis. Throughout this series of complete dotMemory tutorial, we will use this language with a C # application, as an example. It mimics the classic Conway's Game of Life, you probably know. If not, read on Wikipedia. It will not take much time, but will become much easier to understand tutorial. So, before we begin, please download the application from github. Download: // TODO: initial version of Conway's Game of Life

 

        Step 1. Run DotMemory

       1. As to download and install dotmemory, and so this will not go, because the Internet is already a lot. Dotmemory open the main window, let us now begin a profiling session now.

 

 

 

 

        

     2. Select "Local" in the left panel, and then in the "Profile Application", select "Standalone".

     3. Now we should analyze the configuration session options in the right panel.

      1) In the right panel, specify the path you just download the executable file of Conway's Game of Life

      2) Select "Collect memory allocation and traffic data from start" option, this role is to tell dotmemory allocation call stack to start collecting data immediately after starting the application.

  4. Click the "Run" button to start the analysis session. This will run our application and open a new analysis of the label in dotmemory.

    Step 2. take a snapshot

    一旦应用程序运行,我们就可以获得内存快照。在这个操作中最重要的事情是选择合适的时机获取想要的快照。您还记得,快照是应用程序托管堆的即时映像。因此,在拍摄快照之前,首先要做的是使应用程序处于感兴趣的位置。例如,如果我们想查看在生命游戏启动后创建的对象,我们必须在应用程序中执行任何操作之前获取快照。相反,如果我们需要知道哪些对象是动态创建的,则必须在单击应用程序中的“Start”后拍摄快照。

  1.假设我们需要获取生命游戏运行时分配的对象的信息。因此,单击应用程序中的“Start”按钮,让游戏运行一段时间。

  2.点击dotmemory中的“Get Snapshot ”按钮。这将捕获快照数据并将快照添加到快照区域。获取快照不会中断这个分析监视的进程,因此允许我们获取其他的快照(可以继续点击 “Get Snapshot”获取其它快照)。

      

       3.关闭康威生命游戏窗口,结束我们刚才的内存分析监视会话。

       4.看看DotMemory。现在,主页面包含了包含基本信息的单个快照。143.90 MBtotal 意味着应用程序总共消耗143.90 MB的内存。这个大小等于Windows任务管理器的" Commit size":进程请求的内存量。主要包括:

  1)非托管内存:分配在托管堆之外而不被垃圾收集器管理的内存。一般来说,这是.NETCLR、动态库、图形缓冲区(特别是对使用图形的WPF应用程序特别大)所需的内存,等等。无法在分析器中分析这部分内存。

  2).NET total:托管堆中的内存总量,包括空闲内存(要求了但不被应用程序使用的)。

       3).NET used:应用程序使用了的托管堆中的内存,这是唯一一部分内存,.net能允许你用的.因此,这也是唯一一部分你能在dotmemory这个内存分析器中你能分析的内存。

       请单击快照1链接。让我们更详细地看一下快照。

    Step 3. 认识快照概览 

     打开快照后,首先看到的是“Inspections”视图。此页显示主要快照热点。

  您能在这里看到的:

  1)Largest Size: 该图显示了消耗内存的主要部分的对象类型。

  2)Largest Retained Size::这个图表示的意思可以参考博客https://www.jianshu.com/p/aaddf00a1d83,对于gc和可达不可达对象写的很详细。

  3)String duplicates, Sparse arrays, Event handlers leak:dotmemory通过一些角度,自动的检查快照的大多数通用类型的内存,如果你不知道怎么开始,这些自动检查的结果图就是一个好的入口点。

  4)Heap Fragmentation:该图显示托管堆段的碎片:Generation 1, 2和大对象堆。.net的GC机制会进行Generation(分代),可以参考博客https://blog.csdn.net/zhang_hui_cs/article/details/86653202,具体可以自己学习,本篇博文毕竟主要说dotmemory

  让我们继续研究快照并查看它包含的对象:

  1.单击“Type”按钮。这将按类型对快照中的所有对象进行分组,并显示“按类型分组”视图。

 

     学完了基础概念,现在我们可以熟悉dotmemory用户界面和整个内存分析“全家桶”了。

 Step 4. 内存分析入门 

       在我们进一步深入之前,让我们先讨论下对象是如何存储在内存中的。这是有必要的,为我们更好地理解dotmemory所展示给你的东西。

  内存中对象 

    应用程序消耗的内存的大部分被分配给应用程序的对象。对象存储数据并引用其他对象。对象及其引用构成对象图。例如,照片类的对象将存储长值类型的ID字段,以及其他的一些字段(如List<PhotoComment> _comments)。

    class Photo
    {
        long _id;
        String _title;
        User _user;
        List<PhotoComment> _comments;
    }

    

 

 应用程序根 

 当应用程序需要内存时,.NET的垃圾收集器(GC)决定并删除不再需要的对象。要做到这一点,GC将从根开始, 即静态字段、局部变量和外部句柄,开始对每个对象进行链式的GC搜索。如果不可达对象,则被认为不再需要,并且从内存中删除。在下面的示例中,对象D和F将从内存中移除,因为它们不能从应用程序的根访问到。

 

  Retention 

  这里我们来谈谈留用的关键概念。 

  从根到一个对象的路径可能会穿过许多其他对象。如果对象B的所有路径都通过对象A,那么A是B的支配者。换句话说,B只在A.内存中留用,如果A是被GC的,B也将被GC。这就是为什么每个对象最重要的参数是它留用的对象的大小。在dotMemory中,这个参数称为"Retained bytes"。例如,下面示例中的对象c保留632字节。对象B并非由C独占留用;因此,它不包括在计算中。

 

 

 

 让我们返回到dotmemory,看一看刚才打开的“按类型分组”的视图。此视图当前显示堆中的所有对象,由它们独占留用的内存大小排序。正如你所看到的,主要部分由“System.Windows.Shapes.Ellipse”留用(很明显,这些是我们用来可视化生命细胞游戏的椭圆形状)。该类型的对象留用11871980字节的内存,同时消耗3862600字节。

 

 

  一旦你熟悉了主要的分析术语,让我们来看看我们如何使用DotMemory。

  熟悉用户界面 

      我们想让您把使用DotMemory想象成一种调查,比如某种犯罪调查(用dotMemory进行内存分析)。这里的主要思想是收集数据(一个或多个内存快照),并选择一些嫌疑人(可能导致问题的分析对象)。所以,你从一些嫌疑犯名单开始,逐渐缩小这个名单。一个嫌疑犯可能会把你引向另一个嫌疑犯,以此类推,直到你确定真正的罪犯为止。

  1.请看DotMemory的左侧窗口。这是一条分析路径,显示了你所有的调查步骤。

           

 

  分析路径中的每个项目都是您分析的主题。正如您所看到的,您从分析GAMOFLIFE .EXE(步骤步骤1)开始,然后打开快照(1)(步骤2),并要求dotmemory显示堆中的所有对象(对象集所有对象)。即使是一个很小的应用程序也会创建许多对象,因此尝试分别分析每个对象将不会非常搞笑。这就是为什么你在使用dotmemory中分析的主要主题是通过所谓的对象集。

  对象集是由特定条件选择的多个对象。为了便于理解,可以将对象集看作某个查询的结果(非常类似于sql查询)。例如,您可以告诉dotMemory,比如“选择SomeCall()创建的所有对象,或者“选择由实例A留存在内存中的所有对象”等等。

  2.每个对象集可以从不同的视角被视作视图。看看屏幕。您看到的视图称为“按类型分组”,它显示按类型分组的集合中对象的简单列表。其他视图可以显示有关选定集的其他信息。您可以使用屏幕顶部的按钮轻松更改视图:

  如上所述,你分析的每一个主题可能会引导你进入另一个主题。例如,我们看到System.Windows.Shapes.Ellipse类保留了大部分内存,我们想知道创建这些椭圆的方法是什么。让我们看看它。

       3.双击System.Windows.Shapes.Ellipse或打开这些对象的上下文菜单(单击鼠标右键),然后选择“Open this object set”。

      4.选中“Back Traces”视图

  该视图表明,我们的椭圆起源于Grid.InitCellsVisuals() 这个方法。请注意,分析路径现在还包含一个步骤:按system.windows.shapes.elliple类型分组。

 

 

       

 

 

 

 

Guess you like

Origin www.cnblogs.com/rodean/p/11619570.html