Lorenzo B
Lorenzo B

Reputation: 33428

Monotouch: garbage collector, managed and unmanaged objects

I'm trying to understand how MT GC works to avoid memory leaks in iOS apps using (MonoTouch) MT.

As I understood (correct me if I'm wrong), MT memory management works in this manner: each object has a flag which says: "Dear GC, now I'm free to be released whenever you want". When the GC runs, it verifies that flag and remove the object from the memory. So, MT puts every object in a sort of limbo where objects in it will be released (next event cycle, maybe). It's a sort of autorealease mechanism. But it also possible to release explicity an object calling its dispose method. In this case, it means adopt retain-release mechanism.

Reading about MT, I've seen that there are objects that go into the managed heap (for example references to images) and other that go into unmanaged heap (for example images). In the first case (the managed one) I have not to be worry about it, the GC works well. In the second one (unmanaged case), I have to release memory explicity. Why this difference? Could you explain me how I can distinguish managed from unmanaged objects and when to release explicity the memory calling dispose method?

Thank you in advance.

Upvotes: 0

Views: 770

Answers (1)

poupou
poupou

Reputation: 43553

Your description is not quite right. Take the time to read Microsoft documentation about the GC (not GB ;-) and .NET, then read about Mono's current GC (and it's next version - even if it is not yet used for MonoTouch).

Once the above is clear you'll see the common problem when a small managed object can cause some problems (but not leaks) when it represent a large unmanaged object. Using IDisposable can solve this since it gives you more control when objects are finalized.

There's detailed documentation on how (and when) to use this.

Upvotes: 1

Related Questions