5

We have application that runs 24h per day and 7 days per week. Sometimes CPU go to 100% and then go back to 80%. The same with RAM. Is it smart to manually call GC.Collect after few hours or betterto leave it automatically.

We are using C# 2010, SQL 2008 and Fluent Nhiberanet. This is desktop application.

Yahia
  • 69,653
  • 9
  • 115
  • 144
senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • 1
    in general you would not need to call it if the application is written properly. Said so, in some cases we used to call it in our web application especially after allocation of byte[] > 50 MB and we noticed memory was released sooner, but CPU usage went higher during the collection. – Davide Piras Jan 23 '12 at 13:58
  • 2
    try looking at http://blogs.msdn.com/b/scottholden/archive/2004/12/28/339733.aspx It explains when and when not to call GC.Collect. – codingbunny Jan 23 '12 at 13:59
  • Desktop application running 24/7? Why not a service? – Oskar Kjellin Jan 23 '12 at 14:02
  • I would turn your question around and ask why do you think you need to call it? – Chris Dunaway Jan 23 '12 at 15:29

6 Answers6

11

I wouldn't call it smart to call GC.Collect() "every few hours", or "when RAM usage goes to high", but I'd call it smart to call it whenever you are in a position of having more information than the GC, some exmaples

  • You know, this big chunk of RAM or these many small objects you just allocated, will not be used again and you are in a singlethreaded environment and (ofcourse) you have cleared all your references
  • You know, that a "GC break" will hurt less right now, than a bit later

The GC is a highly optimized peace of code and quite smart, but it can only work on information it has.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
2

Manually call the GC.Collect is never a good idea as you should investigate why your app is getting that much resources instead of clean them up every time you are about to reach 100%

Have a look at the below I think it really worth a read

Chapter 5 — Improving Managed Code Performance

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
1

normally the framework itself will handle calling the GC when it's needed you could try to run it without calling it yourself for a day

Volker Mauel
  • 514
  • 3
  • 21
1

GC.Collect won't magically solve problems if you hold unnecessary references or forget to unsubscribe from delegates. The framework collects garbage by itself from time to time, so I don't believe calling GC.Collect every few hours can change anything.

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
0

Short answer: no.

The garbage collector is not an area you want to be going into unless you have to. Normally the .net runtime does a pretty good job of calling it whenever it's needed. If you call it yourself it will just be additional overhead.

linkerro
  • 5,318
  • 3
  • 25
  • 29
0

I would refrain from calling GC.Collect - exceptional cases as described here and here aside.

IF you have any application running 24/7 then I would recommend the following:

  • check real hard for memory leaks and correct any such leak (using multiple memory profilers)
    IF you need any links please say so...

  • try your very best to reduce resource usage by optimizing/rewriting your code

  • configure the application to use GC in "server mode" as that is designed for 24/7 situations (for details see here)
    This is not a miracle solution but something you should try with your application and compare whether it gives you any benefits.

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144