是什么原因导致的.NET内存碎片 [英] What causes memory fragmentation in .NET

查看:389
本文介绍了是什么原因导致的.NET内存碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用红色盖茨蚂蚁内存分析器来调试内存泄漏。它一直警告我说:

I am using Red Gates ANTS memory profiler to debug a memory leak. It keeps warning me that:

内存碎片可能会导致   .NET来RESERVER太多的可用内存。

Memory Fragmentation may be causing .NET to reserver too much free memory.

内存碎片是影响可分配最大对象的大小

Memory Fragmentation is affecting the size of the largest object that can be allocated

由于我有强迫症,这一问题必须得到解决。

Because I have OCD, this problem must be resolved.

哪些标准编码做法有助于避免内存碎片。 你可以通过一些.NET方法整理呢?难道它甚至帮助?

What are some standard coding practices that help avoid memory fragmentation. Can you defragment it through some .NET methods? Would it even help?

推荐答案

您知道,我有点怀疑这里的内存分析器。在.NET中的内存管理系统实际上是试图通过各地的内存移动(这就是为什么你需要针内存为它与外部DLL共享)进行碎片整理的堆给你。

You know, I somewhat doubt the memory profiler here. The memory management system in .NET actually tries to defragment the heap for you by moving around memory (that's why you need to pin memory for it to be shared with an external DLL).

大内存分配的时候很容易出现更多的碎片。而小临时(短期)内存的要求也不会导致碎片在.NET。

Large memory allocations taken over longer periods of time is prone to more fragmentation. While small ephemeral (short) memory requests are unlikely to cause fragmentation in .NET.

下面的东西也值得我们思考。随着时间分配密切.NET,当前内存GC,通常间隔紧密的空间。这是碎片化的对立面。即您应该分配的内存,你打算访问的方式。

Here's also something worth thinking about. With the current GC of .NET, memory allocated close in time, is typically spaced close together in space. Which is the opposite of fragmentation. i.e. You should allocate memory the way you intend to access it.

它是一个管理code只或做它包含的东西如P / Invoke的,非托管内存(Marshal.AllocHGlobal)或东西像GCHandle.Alloc(OBJ,GCHandleType.Pinned)?

Is it a managed code only or does it contains stuff like P/Invoke, unmanaged memory (Marshal.AllocHGlobal) or stuff like GCHandle.Alloc(obj, GCHandleType.Pinned)?

这篇关于是什么原因导致的.NET内存碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆