Often I find calls to GC.Collect() in code, for example, when working with graphs via GDI +.

In smart books they write that you never need to call it yourself.

Actually the question is, are there any justified cases when you need to call it or is it a sign of a bad thing when?

    1 answer 1

    Usually not needed. Sometimes it makes sense to do it:

    1. After the destruction of a large number of objects (for example, closing a form with a large number of elements)

    2. When the application has distinct periods of activity and downtime. If you forcefully trigger garbage collection during an idle period, the likelihood that it will occur during the period of activity and slow down the execution of the code will decrease.

    References:

    When to call GC.Collect ()

    When is it acceptable to call GC.Collect?

    • Under a large number of objects is understood because the objects, the destruction of which is supposed to free up a lot of memory? And that for example the array from 1000 packed in Object weighs the trifle, BUT it is a lot of objects. - iluxa1810
    • @ iluxa1810 Most likely, yes, it is mainly the amount of memory that matters, not the number of objects. Although arrays of a large number of small structures packed into an object can also take up a lot of memory, because some kind of service block is allocated for each object. - MSDN.WhiteKnight