Custom KeyBoard get terminated due to memory pressure in iOS 8

瘦欲@ 提交于 2019-11-29 02:16:58

You can dealloc some things in ViewWillDisappear function of KeyboardViewController

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!