问题描述
我的.net插件中出现OutOfMemory异常.该插件正在使用大量托管和非托管对象.有没有办法捕获此异常?
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?
已更新:我认为任何应用程序如果处理大量需要处理且不定期释放的数据,都可能会收到OutofMemory异常.假设我有一种分析内存中某些结构的方法.我给它300个结构,它将每个结构加载到内存中,对其进行分析并完成处理.在此操作中,内存使用量为X.现在,如果我给它处理3000个结构,它将分析内存中的3000个结构,内存使用量将变为10倍.
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.
已更新
此问题仅在Excel 2007中出现,而在Excel 2003中则没有,这就是为什么我要求MSDN人员进行调查.
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.
推荐答案
在.Net中,当无法为应用程序分配请求的内存时,运行时将抛出OutOfMemoryException.因此,处理这种异常的实际工作很少.
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.
这篇关于如何在.NET(Excel加载项)中捕获OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!