Umar Meer
Umar Meer

Reputation: 11

Can we force garbage collector to run in ASP.net?

I'm new to ASP.Net.

I want to run garbage collector forcefully, but I don't understand how to run the garbage collector.

Upvotes: 0

Views: 710

Answers (4)

Michael Adamission
Michael Adamission

Reputation: 493

My current understanding is that in trying to do this it's likely that you'll end up accidentally calling the garbage collector far too often, causing you unintended consequences. The reason is because in the lifecycle of an ASP.Net Request, the only logical place to call the garbage collector would be at the end of the request, but a site can have hundreds of requests per second, so your site would not scale well because it would be garbage collecting all the time. Your next thought as a developer might be to make a background job for it, but that's an advanced and debatable topic in ASP.Net and besides, the GC itself already runs in a more efficient background job of it's own.

What you want to do instead, is make sure to call Dispose() on any objects at the end of the request, or earlier if you're sure you're done with them (it may be hard to know if you're done with them. An object's life sometimes needs to live on longer than you realize, like with an EF datacontext you don't want to Dispose it too soon in order to let it provide lazy loading on a model's relationships later in the request lifecycle).

One exception to this rule might be if there is a certain type of request that doesn't run very often (like an administrator that kicks off something that consumes a lot of memory) and you also can't dispose it like normal. In that case it would probably be fine to use the GC.Collect mentioned, but use the documentation DhruvJoshi referenced to try to call it 'non-blocking' and use a memory profiler to determine the lowest gen number you can collect.

Upvotes: 0

CharithJ
CharithJ

Reputation: 47570

Answer for your question.

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

You don't have to call GC collect forcefully because it is supposed to be executed automatically when there is a memory stress.

Some executes full garbage collection in the server at mid night when there are no many users around. The argument is that, GC.Collect can pause some threads during the garbage collection process and it would be little better to clear all unwanted memory when there is less impact to the user. However, my opinion is, you don't need that unless you have any specific memory issue.

Upvotes: 0

DhruvJoshi
DhruvJoshi

Reputation: 17146

Quoting from MSDN article

Use the GC.Collect method when there is a significant reduction in the amount of memory being used at a specific point in your application's code. For example, if your application uses a complex dialog box that has several controls, calling Collect when the dialog box is closed could improve performance by immediately reclaiming the memory used by the dialog box. Be sure that your application is not inducing garbage collection too frequently, because that can decrease performance if the garbage collector is trying to reclaim objects at non-optimal times.

This is called Induced Garbage collection. You can call Collect in Forced mode or Optimized mode.

Using from the multiple overloads of this method, you can choose to remove all generations of objects or a specific generation of objects.

Normally you can avoid the scenario when you need to call GC, by wisely wrapping short term objects in a using block

Upvotes: 1

CaringDev
CaringDev

Reputation: 8551

You can try e.g. System.GC.Collect(). Also have a look at MSDN. Be warned though, the need to trigger GC manually is usually an indication of a bug / bad design.

Upvotes: 0

Related Questions