Thought I clear up some things regarding the functionality of the Garbage Collector in Windows Phone, which is maybe a little bit misunderstood functionality of the platform among the people coming from other non .NET environments.
First of all, the whole point of garbage collection is to automate memory management on behalf of the developer, taking (almost) all the pain out of handling allocating and releasing memory. As managed developers, we don’t need to know too much what’s happening behind the curtain, but let’s go through some basics anyway, so we can feel more comfortable leaving our valuable memory to be handled by the Compact Framework.
Managed objects can move around the memory, unlike native objects, this is one important thing to understand. The system can compact memory and decide to move things around without asking your permission, so you can’t have memory pointers in traditional sense. Internally there is some methods that the framework uses to disable moving the memory when it’s handling some managed memory buffers, but thats all hidden from average developers.
As there are two types of objects, value and reference types, there is also two ways the framework handles the memory for these objects. For value types compiler checks when they go out of scope, and generates code to destroy and reclaim their memory in very deterministic manner. Reference type objects are a bit trickier. For their memory the GC reclaims the memory when it seems appropriate, and it is not controlled by your code. It follows through how you reference the object and finally when there are no more references left, it will destroy it.
The GC in Windows Phone 7 Mango is so called generational garbage collector. That means that the GC has two queues, gen-0 and gen-1. Gen-0 has those which will have very short life cycle and it’s collected on each GC round, and Gen-1 is for those who reside longer in memory and is collected rarely. Those objects who survive the gen-0 sweep, are promoted to gen-1.