When does garbage collection get triggered in C#?

前端 未结 2 565
一向
一向 2020-12-19 05:34

I read many things about garbage collection like it\'s generation, scope etc but want to know when does the garbage collection gets triggered ? an example will be really hel

相关标签:
2条回答
  • 2020-12-19 06:14

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

    • The system has low physical memory.
    • 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.

    Source: https://msdn.microsoft.com/en-us/library/ee787088%28v=vs.110%29.aspx#conditions_for_a_garbage_collection

    0 讨论(0)
  • 2020-12-19 06:23

    You are not in control of GC and can not reliably predict its behavior. All calls, like GC.Collect are simple messages to VM to start collection, but that does not mean that collection will eventually start right after the line.

    0 讨论(0)
提交回复
热议问题