Click here to Skip to main content
15,867,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As far as i know it is not known when GC fires, so my question is: is it smart to place a timer control in app and set interval on which GC will fire to release memory?

What I have tried:

private void timer1_Tick(object sender, EventArgs e)
        {
            GC.Collect();
        }
Posted
Updated 9-Feb-20 1:05am

I wouldn't do that since it can drastically reduce performances of your application by firing the GC when not needed. Or, you have a very special case which requires you to do that; but then, you should have a thorough understanding of the GC and .NET memory management, and it should be extensively justified and documented. Premature optimization of the GC is never a good idea, imho.

But you can also try it and decide for yourself :)
 
Share this answer
 
Finalizers - C# Programming Guide | Microsoft Docs[^] states:
Quote:
It is possible to force garbage collection by calling Collect, but most of the time, this should be avoided because it may create performance issues.

Unless you have a good reason to call GC.Collect[^], you better just let the GC do its automatic job.
 
Share this answer
 
Comments
Gruja82 9-Feb-20 6:54am    
I understand now, but then maybe i can create some kind of method that determines amount of memory allocated and then fire GC.
Thomas Daniels 9-Feb-20 6:56am    
But why would you do that?
Gruja82 9-Feb-20 6:58am    
For example if app allocated to much memory and start running slower and GC has not run yet.
Thomas Daniels 9-Feb-20 7:01am    
Well, if you have a good reason to believe that the automatic GC firing is insufficient for your application, then you can do it. But as phil.o said, it requires a thorough understanding of the GC.
Richard MacCutchan 9-Feb-20 7:07am    
If your application allocates too much memory (how much is too much?), running the GC will not improve anything.
Quote:
I understand now, but then maybe i can create some kind of method that determines amount of memory allocated and then fire GC.

Don't.
As the others have said, it's a waste of time.

Your estimate of "memory allocated" and it's need to be garbage collected will always be significantly inferior to just letting the system do it when it actually needs to. Many. many applications run without ever having the GC cut in: If you feel that some of your objects need "proper disposal" then implement the IDisposable interface for the class, and either Dispose them when needed, or create them within a using block so the system gets rid of them automatically.

Unless you have a very odd app, you should not need to call the Garbage Collector yourself. Why do you think you need to?
 
Share this answer
 
Comments
Gruja82 9-Feb-20 7:18am    
I just wanted to know does the app can be optimized using GC manually. I always tend to use IDisposable and eventually destructors in my code. Thanks for the answer.
OriginalGriff 9-Feb-20 7:20am    
Almost certainly, you would de-optimise the app if you try!

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