!dumpheap 的结果是错误的

The result of !dumpheap is wrong

我运行命令"!dumpheap -min 62 -max 64 "得到如下结果,我们发现字符串的计数是43,149,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

问题中提供的信息可能不足以明确说明问题的根源。这可能是我无法重现的版本特定问题,或者您的堆已损坏 (运行 !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