Pregunta

I have discussed with some programmers about the GC. One of them told that "GC only collects the unreachable objects when winform is closed.". Here is a case of winform programming.

But as per my understanding, GC collects the unreachable objects when the stack of unreachable objects are large amount and it will collects them even form is not closed.

Please share your knowledge what is right thing here.

¿Fue útil?

Solución

From MSDN, GC is not just run when the program closes

Conditions for a garbage collection

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top