本文介绍了由于iOS 8中的内存压力,自定义KeyBoard被终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义KeyBoard由于iOS 8中的内存压力而被终止

Custom KeyBoard get terminated due to memory pressure in iOS 8

最初我的自定义键盘占用了大约25mb的内存,但是这个内存没有被释放,我解除了键盘。当我们一次又一次打开自定义键盘时内存不断增加,最后由于内存压力而终止。

Initially my custom keyboard is taking around 25mb of memory, but this memory is not deallocated with I dissmiss the keyboard. Memory keep on increase when we open custom keyboard again and again and finally terminated due to memory pressure.

帮我解决这个问题?

推荐答案

我已经尝试了很多方法来避免这个着名的记忆累积问题,但根据我长期的长期试验和错误,在键盘消失之前释放所有内存的最佳和最简单的方法是在 viewWillDisappear exit(0) > KeyboardViewController

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

[更新] 退出(0)非常适合释放所有内存,因为它会终止键盘扩展程序。不幸的是,似乎杀死进程会使iOS不稳定。

因此,最稳定的方法是在 viewWillDisappear 中尽可能多地释放所有已分配的对象。例如,

[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];


  • nil 设置为所有对象视图控制器的属性。

  • Set nil to all object properties of the view controllers.

    aViewController.anObject = nil;
    


  • 其他大型自定义对象


    • 从所有数组,词典等中删除所有添加的对象。

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

    [anArray removeAllObjects];
    


  • 不要使用 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.

    这篇关于由于iOS 8中的内存压力,自定义KeyBoard被终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 10-19 03:29