c# - Large Object Heap Fragmentation 会导致 64 位进程中的 OutOfMemory 吗?

标签 c# .net memory garbage-collection large-object-heap

我正在准备向我的团队介绍 .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();
        }
    }
}
}

最佳答案

自 .NET 4.5.1(以及 .NET Core)起,支持 LOH 压缩,行为可以通过静态类的 GCSettings.LargeObjectHeapCompactionMode 属性设置 GcSettings .

这意味着,LOH 被 GC 压缩。

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

关于c# - Large Object Heap Fragmentation 会导致 64 位进程中的 OutOfMemory 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48281793/

相关文章:

c# - 如何设置mysqldataadapter的输出参数

c# - 尝试连接到 unity3d 中启用 TLS 的代理时 MQTT 超时

php - MySQL session 表方法

c# - 对于使用 couchbase 的解决方案,这是 .NET 自定义角色提供程序的合理方法吗?

c# - 如何将 IList<T1> 分配给 IList<T2>,其中 T1 是 T2 的子类型?

c# - 任务和垃圾收集存在哪些问题?

.net - 两个面板的 MouseLeave 事件

.net - 将字符串解析为整数(无论 hell 还是高水位)

javascript - 从对象中删除属性的真正方法(删除、空、未定义)

python - 内存增长虽然被覆盖