System.OutOfMemoryException because of Large Dictionary

前端 未结 4 1518
天命终不由人
天命终不由人 2021-01-13 06:02

I keep a large cache in a dictionary with value IEnumerable>. I remove items from the dictionary periodically and add

4条回答
  •  佛祖请我去吃肉
    2021-01-13 06:48

    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.

提交回复
热议问题