MonoTouch: App killed for low mem, Why? Live bytes allocation 5 MB top

前端 未结 3 1935
暗喜
暗喜 2021-01-03 10:34

My iPad app is developed in MonoTouch, because I wanted to avoid all the memory management hell, but it doesn\'t seem the case. On the simulator everything works fine, but w

3条回答
  •  囚心锁ツ
    2021-01-03 11:07

    You need to use NSAutoreleasePools around your image creation code, something like this:

    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;
        }
    }
    

    For some APIs that create objects the object will be added to the autorelease pool, and not released until the pool is drained. MonoTouch automatically creates autorelease pools for all user threads (including threadpool, TPL and normal System.Threading threads), but these pools aren't drained until the thread exits (which you can't control in the case of threadpool and TPL threads). So the solution is to use a NSAutoreleasePool around the critical code (the pool will automatically be drained when disposed).

提交回复
热议问题