Avoiding OutOfMemoryException during large, fast and frequent memory allocations in C#

喜你入骨 提交于 2019-11-30 08:45:09

For those interested, here is an update of what I found out with regards to this problem:

It appeared that the best solution was to implement pooling of our chunks to relieve pressure on the garbage collector, so I did this.

The result was that the add-in got slightly further in its task, but unfortunately it still ran out of memory fairly quickly.

Looking in WinDbg again, the only real difference I could see was that our combined managed heap size was consistently smaller, at around 200MB compared to around 250MB before pooling.

It was almost as if the amount of memory available to .NET was decreasing over time, and so implementing the pooling had simply delayed running out of memory.

If this was true the obvious culprit was a COM component which we use to load the data into memory. We do some caching of COM objects to improve repeated access time to the data. I removed all the caching and ensured everything was released after every query of the data.

Now everything looks fine with regards to memory, it is just much slower (which I will have to solve next).

I guess in hindsight the COM component should have been the first suspect for the memory issues, but hey I learned something :) And on the plus side, the pooling will still be useful to decrease GC overhead, so that was worth doing as well.

Thanks for your comments everyone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!