Garbage collector is a piece of code responsible for allocating and freeing unreferenced objects in a managed application. Garbage collection operation is invoked whenever:
- the application calls GC.Collect() method
- CLR cannot allocate memory for an object
- the application loses focus and goes to the background
- the system sends WM_HIBERNATE message to the managed application
If a Windows Mobile or Windows CE device does not have sufficient memory to allocate new objects, the system sends WM_HIBERNATE message to inactive applications; first being the longest inactive one. If WM_HIBERNATE handling by the application results in free memory sufficient for the current allocation requirement, the message is not sent to remaining apps. If the required memory is not reclaimed this way, the system starts terminating running applications by first sending them WM_CLOSE message and if required followed by calling TerminateProcess() call on them. This process also stops as soon as sufficient memory is reclaimed.
After the WM_HIBERNATE message is received by the application, a full garbage collection takes place. Its a good idea to dispose as many objects as possible during the handling of hibernate event.