Alexander Anikin's blog

My personal blog

Predictable memory disposing in Monotouch

leave a comment »

I have tasks:

– do huge memory block allocation in iOS (using Monotouch) application (like list with ~20Mb strings);

– clear list and have predictable memory disposing (using Garbage Collector, it should do this in time, or when we need more mem or etc).

This tasks was born from another big task. I should open huge txt files using QLPreviewController. But all I can see – memory was allocating and never disposing (but this story not about it, this leak Xamarin planning to fix it in 5.3.5 or 5.3.6 release – will wait ).

To check this allocations I used XCode -> Instruments -> Activity Monitor (btw very useful tool – highly recommended!)

My tests :

1. Alloc 20Mb -> check it in Activity Monitor (+20Mb)-> Dispose variable -> Wait and do something with app (alloc more, add, delete items) -> Check -> nothing changes – 20Mb still.

2. Alloc 10*20 Mb items-> Check – my app has almost the memory (if i will try to get more ~10-20Mb  – iOS will close my app) -> Dispose 5 items (~100Mb) -> Waiting (thinking about GC) + do something (thinking about GC again) -> Check  – nothing – ~200Mb

3. The same with 2, but start use GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced); after disposing – Waiting (thinking about GC) + do something (thinking about GC again) -> Check  – nothing – ~200Mb

4. …

n. Wow! -> The same with 3, but after allocation I add GC. AddMemoryPressure (size); and after disposing GC. RemoveMemoryPressure (size); -> Activity monitor shows me that I have memory cleared and available for future allocations.

I can be wrong somewhere and please let me know my mistakes.


Written by Alex Anikin

July 10, 2012 at 9:46 pm

Posted in C#, iOS, Monotouch

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: