-2

I have an application that do complex processing and creates too many objects, I want to free up the memory after the processing is complete.

I'm currently calling GC.collect in my application in a try catch block, and

// this function in a static class

public static void Collect()
{
    try
    {
        GC.Collect
    }
    catch(Exception)
    {
        //
    }
}

Is this the correct way to call the Garbage collector? should I call it directly? what is best use practice?

  • *"creates too many objects"* Why is it too many? what do you hope to achieve here? – TheGeneral Aug 12 '19 at 02:03
  • `GC.Collect()` does not need a `try catch` block. It won't throw an exception. https://learn.microsoft.com/en-us/dotnet/api/system.gc.collect?view=netframework-4.8#System_GC_Collect – mjwills Aug 12 '19 at 03:25
  • `I want to free up the memory after the processing is complete.` Please share a [mcve] of your issue and we can give more useful advice. – mjwills Aug 12 '19 at 03:26
  • As given, your question is too broad. Especially given the fact that the only code you posted isn't even valid C# code. The marked duplicates contain a wealth of information regarding the what, why, and how of `GC.Collect()` and related topics. If after reviewing the existing information, you still have a _practical programming problem_ that you need help with, post a new question, include a good [mcve], explain what that code does, what you want it to do instead, and what _specifically_ you cannot figure out and need help with. – Peter Duniho Aug 12 '19 at 04:29

2 Answers2

-1

C# is a Garbage Collected language. The Garbage Collector allocates resources for you, and reclaims them when they are no longer used.

Unless you have a really good reason, you shouldn't be calling GC.Collect() at all - the Garbage Collector is smart, and will only run when it thinks it needs to.

This can be triggered by a number of situations, as shown in the docs:

Garbage collection occurs when one of the following conditions is true:

  • The system has low physical memory. This is detected by either the low memory notification from the OS or low memory indicated by the host.

  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.

  • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

Importantly, Garbage Collection can cause all running threads to be paused so it can analyze all the objects in your heap to see if they're still needed at all. This takes time, and if you're running it frequently when it's not required, it can negatively affect the performance of your program.

Community
  • 1
  • 1
Zac Faragher
  • 963
  • 13
  • 26
-2

You don't need to wrap it in a public static method, simply call it when you need it:

// Time to release memory!
GC.Collect();

See When is it acceptable to call GC.Collect? about whether or not it's a good idea to call it or not.

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80