使用Windbg来检查内存

Windbg是一款微软开发的调试windows代码的工具,水很深,不过使用windbg来进行clr的调试则比较简单,windbg使用之前需要进行配置。

File->Symbol path-> SRV*C:MyLocalSymbols*http://msdl.microsoft.com/download/symbols

windbg的下载地址:http://www.windbg.org/
这里说明一下,下载32位即可,可以运行在64位机器上,如果下载64位可能会导致加载sos的异常进而无法调试。

调试命令说明(SOS.dll):http://msdn.microsoft.com/en-us/library/bb190764

第一步是加载必要的dll用来调试:.loadby sos clr。好的,下面就开始我们的查看内存托管代码的旅程。

首先是eeheap -gc,通过这个命令可以知道现在垃圾回收堆中数据的基本情况。EEHeap在SOS.dll中,定义这个命令是用来显示内部CLR进程消耗内存的情况,gc这个命令是用来表示显示的是垃圾回收里面的情况,如过选择-loader则是显示各个域的数据信息;通过这个命令发现一下到底是gc里面的数据是否异常的大。

如果是,那么接着是dumpheap -stat,这个命令是将gc里面的对象一一开列出来,什么类,占了多少空间。但是别太高兴,因为对于引用对象,对象本身是很小的,关键是引用的对象的判断。所以通过这个我们只能看到是什么占了大空间

image

image

通过上面的图片,我们可以发现System.Byte[]占用了较多的空间,我们推测是Tree对象引起的(因为Tree对象使我们自己定义的)

3.!dumpheap -type winformForWinDbg.Tree
通过这个命令看看tree这个对象里面都定义了些什么,都引用了些什么。SOS中定义这个命令的意义是罗列出所有的名字匹配“winformForWinDbg.Tree”的类所在的内存地址
image

下面有我的代码,大致的逻辑是创建6棵树,这些树没有释放。上下两个列表,上面的列表描述的详细信息,下面的列表描述的一个汇总的信息,可以看到MT值为00406bdc的对象有6个,MT值相同代表着他们的对象类型相同,于是在下面的汇总信息表中我们可以看到6个Segment对象被汇总到了一起。

在实际使用中,如果LOH你判断是来自自己的工程对象,搜索一下工程的命名空间,这样工程内部的对象就会呈现出来。如果是.net的自带对象,这个就有点儿麻烦。

4. !objsize 0224519c
0224519c是明细列表中6个叶子节点中的一个(addresss),依次通过这个命令来获取占用内存数,发现每个都是在100M左右,基本可以判断就是这个Leaf节点没有很好的释放,造成了内存泄露。

问题已经判断出来的,但是如果想要继续使用windebug玩下去,还有的聊。

5.!dumpobj 02213840
通过dumpheap -type我们一共返回了9个对象,6个是leaf对象,那么其他的三个是什么呢?好奇者的天下。
image

嗯,通过名字我们可以看出来是一个Queue对象,那么这个Queue里面是神马对象呢?注意看下面列表里面有一个m_head类(value值就是内存地址即address),通过访问这个对象也许可以揭开答案。

6.!do 02213858(注:do的意思就是dumpobj)
image

发现了吗,Queue里面实际是一个Segment类,毫无疑问这个Segment是一个系统生成的中间类,那么探究里面的东西,就是下一步要做的,这回我们看到没有了m_head,取而代之的是m_arry,这说明Segment是一个数组对象(当然更靠谱的方式是看Type列,object[]),那么命令跟着也变了:

7. !dumparray 02213880

理论上面讲,我应该会获得一个31个数组单项,其中前六项是有地址的,地址应该和之前的6个叶子节点的地址一致。可惜我只是得到了31个空的数组,原因不详。但是至此通过Windbg来看C#对象已经很清晰了。以后再面对内存泄露的问题也有应手工具了。

文章借鉴自那些年黑了你的微软BUG

附录:代码段(我将GC回收那一段注释掉了,尽管我本地是Framework4,但是如果手工调用GC仍然没有重现内存泄露的现象,所以索性去掉了,第七步没有得到数组值不知道和这个是否有关系)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;

namespace winformForWinDbg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static ConcurrentQueue<Tree> _leakedTrees = new ConcurrentQueue<Tree>();

        private static void VerifyLeakedMethod(IEnumerable<string> fruits, int leafCount)
        {
            foreach (var fruit in fruits)
            {
                Tree fruitTree = new Tree(fruit);
                BuildFruitTree(fruitTree, leafCount);
                _leakedTrees.Enqueue(fruitTree);
            }

            Tree ignoredItem = null;
            while (_leakedTrees.TryDequeue(out ignoredItem)) { }
        }

        private static void BuildFruitTree(Tree fruitTree, int leafCount)
        {
            Console.WriteLine("Building {0} ...", fruitTree.Name);

            for (int i = 0; i < leafCount; i++) // size M
            {
                Leaf<byte[]> leaf = new Leaf<byte[]>(Guid.NewGuid())
                {
                    Content = CreateContentSizeOfOneMegabyte()
                };
                fruitTree.Leaves.Add(leaf);
            }
        }

        private static byte[] CreateContentSizeOfOneMegabyte()
        {
            byte[] content = new byte[1024 * 1024]; // 1 M
            for (int j = 0; j < content.Length; j++)
            {
                content[j] = 127;
            }
            return content;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> fruits = new List<string>() // 6 items
              { 
                "Apple", "Orange", "Pear", "Banana", "Peach", "Hawthorn",
              };

            VerifyLeakedMethod(fruits, 100); // 6 * 100 = 600M

            //GC.Collect(2);
            //GC.WaitForPendingFinalizers();
        }
    }

    internal class Tree
    {
        public Tree(string name)
        {
            Name = name;
            Leaves = new List<Leaf<byte[]>>();
        }

        public string Name { get; private set; }
        public List<Leaf<byte[]>> Leaves { get; private set; }
    }

    internal class Leaf<T>
    {
        public Leaf(Guid id)
        {
            Id = id;
        }

        public Guid Id { get; private set; }
        public T Content { get; set; }
    }
    
}
原文地址:https://www.cnblogs.com/xiashiwendao/p/3258237.html