8

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 I dissmiss the keyboard. Memory keep on increase when we open custom keyboard again and again and finally terminated due to memory pressure.

Help me out with this issue?

Rachit
  • 814
  • 9
  • 19

3 Answers3

1

You can dealloc some things in ViewWillDisappear function of KeyboardViewController

Tatiana
  • 390
  • 8
  • 23
1

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.

Mark Johnson
  • 1,105
  • 9
  • 8
1

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.

NeonBerry
  • 372
  • 2
  • 6
  • I'm dealing with this issue like you. I've tried `exit(0);` its super effective. It simply kills the process. However it will make the keyboard slow everytimes call it. I think the iOS has caches the keyboard to have faster process. I've tried to remove all subviews, constraints, but it seems the process is still there, and memory still increase everytime i use the keyboard :( – TomSawyer Jan 29 '16 at 18:43
  • In general, all custom "objects" you've created should be removed in viewWillDisappear before disappeared. But the point is you should remove all custom "objects", not just custom views and constraints in order to prevent iOS memory leak and eventually keyboard crash. Check if you also removed arrays, dictionaries, sets or whatever objects you've created. It's really annoying but works. – NeonBerry Feb 01 '16 at 06:12
  • I've removed the all objects included : class instance, constraints, views, dictionaries ... And some apps has failed to run like (viber) , i don't know why. I have to leave some variables. And if i already removed all views, do i need to remove also constraints and gesture? Basically, if i removed the views, constraints are automatically be removed too. – TomSawyer Feb 01 '16 at 14:39
  • BTW, have you removed all custom view hierarchy? That is, custom subviews of a custom view should be manually removed. The case of arrays or else are the same. Subarrays of an array should be removed manually.Also, all custom constraints should be removed manually. Gestures have very minor size but need to be removed manually also if memory is still being increased. Very annoying but no choice..., thanks to iOS. – NeonBerry Feb 02 '16 at 15:32