[英]Can you give me an example that causes memory fragmentation in .NET
I'm working to make our application more performant by caching more stuff in memory. 我正在努力通过在内存中缓存更多内容来使我们的应用程序更高效。 What worries me, though, is all that I'm reading about how the large object heap is not really compacted during a garbage collection, and that this can cause memory fragmentation.
但是,令我担心的是,我正在阅读的有关大型对象堆如何在垃圾收集过程中没有真正压缩的情况,以及这会导致内存碎片化。
I've been doing some small testing, but it seems I can't induce this problem. 我一直在做一些小测试,但似乎我无法引发这个问题。 So here's my question: Can you show me a code snippet in C# that would, at some point, cause failure due to memory fragmentation?
所以这是我的问题:你能告诉我C#中的代码片段,在某种程度上会因内存碎片导致失败吗?
Try having a look at the code snippet in this article The Dangers of the Large Object Heap and implementing this code just after the catch block of the Fill function to fragment the LOH, as outlined by cfneese posted on 11/04/2011 in the comments for the bug Large Object Heap fragmentation causes OutOfmemoryException : 试着看一下本文中的代码片段大对象堆的危险,并在填充函数的catch块之后实现此代码,以便对LOH进行分段,如cfneese在2011年4月11日发表的评论中所述对于错误大对象堆碎片导致OutOfmemoryException :
unsafe
{
var w = new StreamWriter(@".\test.txt");
for (int i = 0; i < count; i++)
{
var handle = GCHandle.Alloc(smallBlocks[i], GCHandleType.Pinned);
w.WriteLine(String.Format("{0,10}\t{1,10}", i, handle.AddrOfPinnedObject()));
handle.Free();
}
w.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.