The .NET Compact Framework, just like the full .NET framework, features a garbage collector that automatically runs on its own to free up unused memory to the operating system. The garbage collector runs when any of the following conditions are met:
When an application is moved to the background, a garbage collection is run.
A garbage collection is automatically initiated after 1 MB of memory is allocated since the last garbage collection.
GC.Collect()
You can call the GC.Collect()
function in managed code to force a garbage collection.
When the system fails to allocate or reallocate memory, a garbage collection is automatically initiated by the .NET Compact Framework to attempt to free up more memory.
The following diagram describes the lifecycle of an object and how it is eventually collected by the garbage collector:
Garbage collection is a somewhat expensive process—it needs to suspend threads, mark live objects, and so on. Forcing a GC.Collect()
call frequently to free up memory is, therefore, not a good practice and may even lead to a significant decrease in performance. If your application runs out of memory more often than usual, you should ensure that your objects have been properly disposed (call the .Dispose()
method if it is available in the object). The garbage collector should be left to run automatically.
3.12.36.30