简体   繁体   English

你能给我一个在.NET中导致内存碎片的例子吗?

[英]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.

相关问题 什么导致.NET中的内存碎片 - What causes memory fragmentation in .NET 当我使用“显示器”时,因为不能用“锁定”来完成,您能举一个简单的例子吗? - Can you give me a simple example when I am to use “monitor” because it cannot be done with “lock”? 固定的内存导致C#WebApi中的碎片 - Pinned memory causes fragmentation in C# WebApi 可以举个例子,说明何时应该使用UIElement.UpdateLayout()? - Can give me an example that when should use UIElement.UpdateLayout()? 任何人都可以通过示例给我Dotnetnuke模块本地化 - Can any one give me Dotnetnuke module localization with example 内存碎片? - Memory fragmentation? 您能举一个WebClient下载网站源代码并异步过滤某些内容以免冻结GUI的示例吗? - Could you give me an example of a WebClient downloading a website source code and filtering something asynchronously as to not freeze the GUI? 谁能给我一个使用BouncyCastle将.pem公共DSA密钥导入c#的示例吗? - Can anyone give me an example of using BouncyCastle to import .pem public DSA key into c#? SharpDX 内存碎片 - SharpDX memory fragmentation WPF内存碎片 - WPF memory fragmentation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM