LOH 碎片化 - 2015 年更新

Tho*_*ler 2 .net c# debugging windbg out-of-memory

有很多关于 .NET LOH 的信息,并且已经在多篇文章中进行了解释。但是,似乎有些文章缺乏一点精确度。

过时的信息

Microsoft 项目经理 Brian Rasmussen 的回答 (2009) 中,他说限制是 85000 字节。他还让我们知道有一个更奇怪的例子,double[]大小为 1000 个元素。CLR 团队成员 Maoni Stephens (MSDN, 2008)声明了相同的 85000 限制。

在评论中,Brian Rasmussen 变得更加准确,让我们知道它可以用byte[]85000 字节 - 12 字节的a 进行复制。

2013年更新

Mario Hewardt(“Advanced Windows Debugging”的作者)在 2013 年告诉我们,.NET 4.5.1 现在也可以压缩 LOH,如果我们告诉它这样做的话。由于默认情况下它是关闭的,除非您已经意识到它,否则问题仍然存在。

2015年更新

我不能再重现这个byte[]例子了。使用简短的蛮力算法,我发现我必须减去 24(byte[84999-24]在 SOH 中,byte[85000-24]在 LOH 中):

    static void Main(string[] args)
    {
        int diff = 0;
        int generation = 3;
        while (generation > 0)
        {
            diff++;
            byte[] large = new byte[85000-diff];
            generation = GC.GetGeneration(large);
        }            
        Console.WriteLine(diff);
    }
Run Code Online (Sandbox Code Playgroud)

我也无法重现该double[]声明。蛮力给了我 10622 个元素作为边界(double[10621]在 SOH 中,double[10622]在 LOH 中):

    static void Main(string[] args)
    {
        int size = 85000;
        int step = 85000/2;
        while (step>0)
        {
            double[] d = new double[size];
            int generation = GC.GetGeneration(d);
            size += (generation>0)?-step:step;
            step /= 2;
        }
        Console.WriteLine(size);
    }
Run Code Online (Sandbox Code Playgroud)

即使我为较旧的 .NET 框架编译应用程序,也会发生这种情况。它也不依赖于发布或调试版本。

如何解释这些变化?

Tho*_*ler 5

byte[]示例中从 12 位变为 24 位可以通过 CPU 架构从 32 位变为 64 位来解释。在为 x64 或 AnyCPU 编译的程序中,.NET 开销从 2*4 字节(4 字节对象头 + 4 字节方法表)增加到 2*8 字节(8 字节对象头 + 8 字节方法表)。此外,该数组具有 4 字节(32 位)与 8 字节(64 位)的长度属性。

对于double[]例如,只使用一个计算器:85000个字节/ 64位的双类型= 10625项,这已经是接近。考虑到 .NET 开销,结果是(85000 字节 - 24 字节)/每个双精度 8 字节 = 10622 双精度。所以没有任何特殊处理了double[]

顺便说一句,我之前从未找到任何 LOH 碎片的工作演示,所以我自己写了一个。只需为 x86 编译以下代码并运行它。它甚至包括一些调试提示。

它在编译为 x64 时效果不佳,因为 Windows 可能会增加页面文件的大小,因此后续 20 MB 内存的分配可能会再次成功。

class Program
{
    static IList<byte[]> small = new List<byte[]>();
    static IList<byte[]> big = new List<byte[]>(); 

    static void Main()
    {
        int totalMB = 0;
        try
        {
            Console.WriteLine("Allocating memory...");
            while (true)
            {
                big.Add(new byte[10*1024*1024]);
                small.Add(new byte[85000-3*IntPtr.Size]);
                totalMB += 10;
                Console.WriteLine("{0} MB allocated", totalMB);
            }
        }
        catch (OutOfMemoryException)
        {
            Console.WriteLine("Memory is full now. Attach and debug if you like. Press Enter when done.");
            Console.WriteLine("For WinDbg, try `!address -summary` and  `!dumpheap -stat`.");
            Console.ReadLine();

            big.Clear();
            GC.Collect();
            Console.WriteLine("Lots of memory has been freed. Check again with the same commands.");
            Console.ReadLine();

            try
            {
                big.Add(new byte[20*1024*1024]);
            }
            catch(OutOfMemoryException)
            {
                Console.WriteLine("It was not possible to allocate 20 MB although {0} MB are free.", totalMB);
                Console.ReadLine();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)