MonoTouch: application killed for low memory, why? 5 MB Direct Byte Allocation

My iPad app was developed in MonoTouch because I wanted to avoid the entire memory admiral, but it doesn't seem to be that way. Everything worked fine on the simulator, but when I tested my application on the device, I was horrified to find that it quickly gets killed by the OS after some memory warnings. My application is a simple image browser, it downloads some PNG images and displays them using some UIView inside a UIScrollView, loading the next or previous one when it gets touched. It works great on a simulator. But on the device, after loading and unloading about 6-11 images, it starts to receive warnings about memory, and then suddenly the process is destroyed. I checked all my instinctive cycles and I will correctly delete all links before uploading new images.

So, I started working with tools and began to profile the memory allocation of my application on the iPad. There I found that Live bytes are only about 5-9 MB, only what I expected, but for some strange reason, the amount of dead memory is almost not collected, because after allocating about 50 MB (less than 5-9 MB Live Bytes), he is killed! Here is a screenshot of my application’s profiling session:

Instruments screenshots documenting memory allocation problem under monotouch

And here is the sequence of heapshots sequences:

heapshots of my app in instruments

There are also small leaks, but I think they are not so large as to be to blame. These are all 48 bytes of leakage from strdup, a known issue when releasing UIScrollView in iOS 5.1:

enter image description here

, , Live Bytes 5 , , 50 iPad 314 iPhone4S, :

memory growth of my App on iPhone4S

- , , , ? monotouch? - , ? , ? , .

//:

    void StartImageLoadingThread()
{
    tokenSource = new CancellationTokenSource ();
    token = tokenSource.Token;
    Task task1 = new Task( () => PerformLoadImageTask(token),token);
    task1.Start();
}


void PerformLoadImageTask(CancellationToken token)
{
        if (token.IsCancellationRequested)
                {
                     Console.WriteLine("Task {0}: Cancelling", Task.CurrentId);
                return;
                }

            current_uiimage_A = UIImage.FromFileUncached(file_name);
            page_A_image_view.Image = current_uiimage_A;


} 


void UnloadImageAFromMemory()
{
      page_A_image_view.Image = null;
      current_uiimage_A.Dispose();
      current_uiimage_A = null;
}   

, ? , , , ? , GC, , , . , .

- Xamarin .

, !

UPDATE: uiviews, Live Bytes 9 5 , - , , .

2: , , ! , MonoTouch , UIImages . , , , ! ?

+3
3

UIImage.FromFile. png- .

GC.Collect , . , GC.Collect. , Image.Dispose : (

, , : (

, ....

+2

NSAutoreleasePools , :

void PerformLoadImageTask(CancellationToken token)
{
    if (token.IsCancellationRequested)
    {
         Console.WriteLine("Task {0}: Cancelling", Task.CurrentId);
         return;
    }

    using (var pool = new NSAutoreleasePool ()) {
        current_uiimage_A = UIImage.FromFileUncached(file_name);
        page_A_image_view.Image = current_uiimage_A;
    }
}

API, , , . MonoTouch ( threadpool, TPL System.Threading), , ( threadpool TPL). , , NSAutoreleasePool ( ).

+3

, , , dispose, ? .

- , .

, , ui, ui , .

, , !

,

+1

All Articles