本文介绍了由于System.OutOfMemoryException的大词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我养大缓存与价值的IEnumerable℃的字典; KeyValuePair<日期时间,双>> 。我从字典中删除项目定期和项目添加到字典中周期性。现在每一次我得到一个System.OutOfMemoryException。我想知道为什么没有垃圾收集器来拯救我?

I keep a large cache in a dictionary with value IEnumerable<KeyValuePair<DateTime, Double>>. I remove items from the dictionary periodically and add items to the dictionary periodically. Every now and again I get a System.OutOfMemoryException. I wanted to know why doesn't the garbage collector come to my rescue?

推荐答案

请记住,一个堆可以得到支离破碎的@Gabe提及。即使你可能有空闲的内存它可能没有大到足以分配字典,当它执行它的调整一大块。

Remember that a Heap can get fragmented as @Gabe mentioned. Even though you might have free memory it might not have a chunk large enough to allocate the dictionary to when it performs it's resize.

也许你可以使用从缓存块模式和实践图书馆这将帮助你实现一个良好的缓存。也许你可以选择一个算法,并不动态分配内存,具有固定数量的条目?

Perhaps you could use the caching block from the patterns and practices library MSDN Library Link that will assist you in implementing a good cache. Maybe you could choose an algorithm that doesn't dynamically allocate memory, with a fixed number of entries?

另外请注意,如果没有,你可以使用任何内存然后它与缓存的大小,而不是垃圾收集器的问题。

Also note that if there isn't any memory that you can use then it's a problem with the size of your cache, not the garbage collector.

这篇关于由于System.OutOfMemoryException的大词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 09:22