由于 iOS 8 中的内存压力,自定义键盘被终止

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

问题描述

由于 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.

帮我解决这个问题?

推荐答案

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

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) 非常适合释放所有内存,因为它会终止键盘扩展进程.不幸的是,杀死进程似乎会使 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;
    

  • 对于其他大型自定义对象

    For other big custom objects

    • 从所有数组、字典等中删除所有添加的对象.

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

    [anArray removeAllObjects];
    

  • 不要使用 imageNamed: 缓存图片.

    如果释放得当,调试时的内存使用量不会增加或仅略微增加(每次关闭<0.1MBytes).如果在多次关闭后内存使用量增加,即使自定义对象被尽可能多地释放,exit(0) 可以定期调用,但存在一定的卸载风险.

    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 中的内存压力,自定义键盘被终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

  • 上一篇:如何确定Android中当前的IME? 下一篇:“没有 id 0 的键盘"- 这是什么意思?

    相关文章

    最新文章