Custom KeyBoard get terminated due to memory pressure in iOS 8

后端 未结 3 630
小蘑菇
小蘑菇 2020-12-16 04:23

Custom KeyBoard get terminated due to memory pressure in iOS 8

Initially my custom keyboard is taking around 25mb of memory, but this memory is not deallocated with

相关标签:
3条回答
  • 2020-12-16 04:42

    You can dealloc some things in ViewWillDisappear function of KeyboardViewController

    0 讨论(0)
  • 2020-12-16 04:46

    I have tried tons of ways to avoid this famous memory accumulation issue, but according to my long long trial & errors, the best and the simplest way to free all memory before a keyboard disappears is to call exit(0) in viewWillDisappear of KeyboardViewController.

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        exit(0);
    }
    

    [Update] exit(0) was perfect to release all memory since it kills the keyboard extension process. Unfortunately it seems like killing the process makes iOS unstable.
    Consequently, the most stable way is to release all allocated objects as much as possible in viewWillDisappear. For example,

    For all custom views and all custom view controllers

    • Remove all strong references of the views and the view controllers, such as subviews, constraints, gestures, strong delegate, and so on.

      [aView removeFromSuperview];
      [aView removeConstraints:aView.constraints];
      for (UIGestureRecognizer *recognizer in aView.gestureRecognizers)
          [aView removeGestureRecognizer:recognizer];
      
    • Set nil to all object properties of the view controllers.

      aViewController.anObject = nil;
      

    For other big custom objects

    • Remove all added objects from all arrays, dictionaries, and so on.

      [anArray removeAllObjects];
      
    • Do not cache images using imageNamed:.

    If well released, memory usage while debugging would not be increased or very slightly increased(<0.1MBytes per dismissing). If memory usage is increased after many dismissing even though custom objects are released as much as possible, exit(0) can be called periodically with some risk of unloading.

    0 讨论(0)
  • 2020-12-16 05:00

    The keyboard extension runs in a process that persists after the keyboard disappears. Your keyboards view controller is created anew each time your keyboard is created, but the process that view controller is in persists. So free memory when your view controller is closed. If you are using images you won't want to use imageNamed: you will want to use imageWithContentsOfFile:. Because UIImage uses a cache for imageNamed that will persist.

    0 讨论(0)
提交回复
热议问题