How to force Garbage Collection


Memory management is a primary thing for any application. The .Net Framework provides a new mechanism for releasing unreferenced objects from the memory ,this process is called Garbage Collection (GC). In some rare situations, forcing a Garbage Collection may improve your application's performance. The garbage collection (GC )class provides a GC.Collect method, which you can use to give your application some direct control over the garbage collector. It might be appropriate to use the GC.Collect method in a situation where there is a significant reduction in the amount of memory being used at a defined point in your application's code.

GC.Collect call is discouraged because it decreases the current performance of the application. Whenever you call the garbage collector to performs a collection , it suspends all currently executing threads. This can become a performance issue if you call GC.Collect more often than is necessary. You should be careful not to place code that calls GC.Collect at a point in your program where users could call it frequently. However, if you can reliably test your code to confirm that calling Collect() won't have a negative impact then go ahead.

It is not guaranteed that the Garbage Collection process will start after calling the method and the memory is release. So, there is still uncertainty whether actually the Garbage Collection will occur or not.
 (C) 2022    Founded by raps mk
SiteMap  | Terms  | About