我应该如何在Global.ascx中的应用程序级别调用垃圾收集器? [英] How should I call garbage collector in application level that means in Global.ascx ?

查看:56
本文介绍了我应该如何在Global.ascx中的应用程序级别调用垃圾收集器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临OutOfMemory异常,但我不可能在所有应用程序级别处置对象。所以请告诉我哪一个是Global.ascx中呼叫垃圾收集的最佳实践。



我找到了类似的东西



1)GC.Collect();

GC.WaitForPendingFinalizers();



2)GC.Collect();

GC.SuppressFinalize(this);

解决方案

除非您的应用程序编写得非常糟糕,否则甚至不必直接调用GC:相反,检查您是否正在处理支持IDisposable的所有对象,因为它不太可能是导致您的实际内存问题:内存不足是一个通用错误,这意味着一些稀缺资源耗尽,它不一定是堆空间。


它们都是不同的东西。 SuppressFinalize(this)调用将阻止垃圾收集器调用指定对象的 Finalize 方法,另见文档 [ ^ ] 。



WaitForPendingFinalizers [ ^ ]将阻止调用线程,直到调用完所有终结器。



但是,我建议你在打电话的时候小心翼翼。在某些情况下,这可能是必要的,但我会先调查为什么你在调用垃圾收集器之前得到一个 OutOfMemoryException

内存不足异常+ C#Winforms [ ^ ]



很好的解释。查看以上链接。这可能有所帮助。


I am facing OutOfMemory exception but it not possible me to dispose object in all application level.So please tell me which one is best practice for call garbage collect in Global.ascx .

I found some like that

1) GC.Collect();
GC.WaitForPendingFinalizers();

2) GC.Collect();
GC.SuppressFinalize(this);

解决方案

Unless your application is written extremely badly, it should not be necessary to even invoke the GC directly: instead, check that you are Disposing of all objects which support IDisposable, as it is unlikely yo be actual memory that is causing your problem: "Out of memory" is a generic error which means that some scarce resource is exhausted, it doesn't have to be heap space.


They are both different things. The SuppressFinalize(this) call will prevent the garbage collector from calling the the Finalize method for the specified object, also see the documentation[^] on that.

The WaitForPendingFinalizers[^] will block the calling thread until all finalizers have been called.

However, I do advise you to tread carefully when calling this. In some cases this might be necessary, but I would first look into why you're getting an OutOfMemoryException before calling the garbage collector.


Out of memory exception + C# Winforms[^]

Nice explanation.Check the above link.This may help.


这篇关于我应该如何在Global.ascx中的应用程序级别调用垃圾收集器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆