5

我正在准备向我的团队介绍 .net GC 和内存。不同的来源讨论了碎片对大对象堆的潜在影响。由于这将是一个有趣的现象,我试图在代码中展示它。

Thomas Weller 提供了这段代码,当试图将更大的对象分配到 LOH 中的已释放间隙中时,它看起来应该会导致 OOM,但由于某种原因它不会发生。LOH 是否在 .net 4.6 中自动压缩?LOH 碎片在 64 位中根本不是问题吗?

来源:https ://stackoverflow.com/a/30361185/3374994

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();
        }
    }
}
}
4

2 回答 2

3

由于 .NET 4.5.1(也是 .NET Core)支持 LOH 压缩,并且可以通过GCSettings.LargeObjectHeapCompactionMode静态类GcSettings的属性设置行为。

这意味着 LOHGC 压缩。

请注意,32 位进程对可以使用多少内存有一些限制,因此它更有可能遇到 OOM 异常。

于 2018-01-16T13:39:41.083 回答
1

我的猜测是,LOH 不会自动压缩。

由于紧凑 LOH 会影响性能,因此我们应该在确定时执行此操作。

使用此代码,它将很快耗尽内存,紧凑操作仅回收未引用的对象

于 2018-01-19T02:48:06.417 回答