How to trap the OutOfMemoryException in .NET (Excel add-in)

牧云@^-^@ 提交于 2019-12-10 21:32:14

问题


I am getting OutOfMemory Exception in my .net addin. The addin is using large number of managed and unmanaged objects. Is there a way to trap this exception?

Updated: I think any application can get an OutofMemory exception if it processess a large amount of data that needs to be processed and not freed periodically. Suppose I have a method that analyses some structures in memory. I give it 300 structures, it loads each of them in memory, analyses them and completes processing. In this operation the memory usage was X. Now if I give it 3000 structures to process, it is going to analyze those 3000 in memory and the memory usage will go to 10X.

Updated

This problem is only seen in Excel 2007 and not in Excel 2003 which I why I have asked a MSDN personnel to look into it.


回答1:


In .Net the OutOfMemoryException is throw by the runtime when it cannot allocate the requested memory for your application. As such there is very little actual work that can be done to handle such an exception.

Instead of catching it you should prevent it from happening in the first place. Ignoring the exception will not make the problem go away.




回答2:


If you get an OutOfMemoryException your program has died, ceased to exist, passed on, it is no more and has gone to meet its maker.

But seriously, it's not able to recover some of the memory it's run out of so you can't do anything.




回答3:


It could be the garbage collector getting you. Try periodically forcing collection.

If that doesn't help, read this: http://www.simple-talk.com/dotnet/.net-framework/the-dangers-of-the-large-object-heap/



来源:https://stackoverflow.com/questions/776080/how-to-trap-the-outofmemoryexception-in-net-excel-add-in

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