Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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);
Posted
Updated 5-Jan-14 23:15pm
v2
Comments
Kornfeld Eliyahu Peter 6-Jan-14 5:13am    
None of them. If it was possible to free memory by garbage collection, the underlying system was call it automatically...
Find the reason for your OutOfMemory exception and resolve that problem...

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.
 
Share this answer
 
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.
 
Share this answer
 
Out of memory exception + C# Winforms[^]

Nice explanation.Check the above link.This may help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900