!dumpheap的结果是错误的

时间:2016-09-05 12:25:44

标签: .net debugging windbg sos

我运行命令“!dumpheap -min 62 -max 64”并找到以下结果,我们发现字符串的计数是43,449,740,但是它们的总大小只有5,146,310字节,所以总大小是错误的, 对?

Statistics:
              MT    Count    TotalSize Class Name
00007fff0faaf518        1          100 System.Runtime.Serialization.Formatters.Binary.InternalPrimitiveTypeE[]
00007fff0fb22c98        2          200 System.Int16[]
00007fff0fb06888       36         3532 System.Byte[]
00007fff0fb02090      174        17124 System.Char[]
00007fff0fb03920      545        54500 System.Int32[]
00007fff0fb00e08 **43149740**      **5146310** System.String
Total 43150498 objects

1 个答案:

答案 0 :(得分:1)

问题中提供的信息可能不足以明确告诉问题的根源。这可能是我无法重现的特定于版本的问题,或者您的堆已损坏(运行!verifyheap)。

以下程序创建长度为64(128字节数据),长度为200(400字节数据)和长度为1024(2048字节数据)的字符串。

using System;
using System.Collections.Generic;

namespace StringSizeDumpheap
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> smallstrings = CreateList(1000, 64);
            List<string> mediumstrings = CreateList(1000, 200);
            List<string> largestrings = CreateList(1000, 1024);
            const string dbginfo = "Debug now. Use !dumpheap -min -max with 0n140/0n144, 0n400/0n440 and 0n2000/0n2200.";
            Console.WriteLine(dbginfo);
            Console.ReadLine();
            // Access strings to prevent optimization
            smallstrings[0] = "";
            mediumstrings[0] = "";
            largestrings[0] = "";
        }

        private static List<string> CreateList(int count, int size)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < count; i++)
            {
                list.Add(new string('x', size));
            }
            return list;
        }
    }
}

使用该演示程序,WinDbg + SOS在WinDbg 6.2.9200上提供了预期的结果(程序编译为.NET 4.5.2,首选32位,调试版本)

0:004> !dumpheap -stat -mt 70dde918        -min 0n140 -max 0n144
Statistics:
      MT    Count    TotalSize Class Name
70dde918     1002       142284 System.String
Total 1002 objects

0:004> !dumpheap -stat -mt 70dde918        -min 0n380 -max 0n500
Statistics:
      MT    Count    TotalSize Class Name
70dde918     1000       414000 System.String
Total 1000 objects

0:004> !dumpheap -stat -mt 70dde918        -min 0n2000 -max 0n2200
Statistics:
      MT    Count    TotalSize Class Name
70dde918     1000      2062000 System.String
Total 1000 objects