title: Understanding Instruments memory allocation log on iOS
body: <p>I have built an iOS app that is almost done, however, I have recently experienced that it crashes after while due to "Memory pressure". So I started profiling the memory allocations in Instruments and sure, the app does use quite a lot of memory and it only seems to increase during usage.</p>
  
  <p>However, relatively new to Instruments memory allocation I am not quite able to decipher where 52 % of the allocations are made, as seen in the screenshot below:</p>
  
  <p><img src="https://i.stack.imgur.com/ECzoa.png" alt="enter image description here"> </p>
  
  <p>It has obviously got something to do with Core Animation, but what exactly is hard for me to determine, so I thought that some clever minds out there might know the answer to that.</p>
  
  <h2>Breadcrumb:</h2>
  
  <p>My app uses custom segues, when moving between view controllers, where a lot of animation is taking place. Here is an example:</p>
  
  <pre><code>@interface AreaToKeyFiguresSegue : UIStoryboardSegue
  
  @end
  
  ...
  
  @implementation AreaToKeyFiguresSegue
  
  - (void)perform
  {
      [self sourceControllerOut];
  }
  
  - (void)sourceControllerOut
  {
      AreaChooserViewController *sourceViewController = (AreaChooserViewController *) [self sourceViewController];
      KeyFigureViewController *destinationController = (KeyFigureViewController *) [self destinationViewController];
  
      double ratio = 22.0/sourceViewController.titleLabel.font.pointSize;
  
      sourceViewController.titleLabel.adjustsFontSizeToFitWidth = YES;
  
      [UIView animateWithDuration:TRANSITION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  
          // Animate areaChooser
          sourceViewController.areaChooserScrollView.alpha = 0;
          sourceViewController.areaScrollViewVerticalSpaceConstraint.constant = -300;
  
          sourceViewController.backButtonVerticalConstraint.constant = 20;
          sourceViewController.backButton.transform = CGAffineTransformScale(sourceViewController.backButton.transform, ratio, ratio);
          sourceViewController.backButton.titleLabel.textColor = [UIColor redKombitColor];
  
          sourceViewController.backArrowPlaceholderVerticalConstraint.constant = 14;
          sourceViewController.backArrowPlaceholder.alpha = 1;
  
          sourceViewController.areaLabelVerticalConstraint.constant = 50;
          sourceViewController.areaLabel.alpha = 1;
  
          [sourceViewController.view layoutIfNeeded];
  
      } completion:^(BOOL finished) {
          [destinationController view]; // Make sure destionation view is initialized before animating it
          [sourceViewController.navigationController pushViewController:destinationController animated:NO]; // Push new viewController without animating it
  
          [self destinationControllerIn]; // Now animate destination controller
      }];
  }
  
  - (void)destinationControllerIn
  {
      AreaChooserViewController *sourceViewController = (AreaChooserViewController *) [self sourceViewController];
      KeyFigureViewController *destinationController = (KeyFigureViewController *) [self destinationViewController];
  
      destinationController.keyFigureTableViewVerticalConstraint.constant = 600;
      destinationController.keyFigureTableView.alpha = 0.0;
      destinationController.allFavoritesSegmentedControl.alpha = 0.0;
      [destinationController.view layoutIfNeeded];
      [sourceViewController.segueProgress setHidden:YES];
  } 
  
  @end
  </code></pre>
  
  <p>And whenever a view controller is to be popped, I simply do the reverse thing:</p>
  
  <pre><code>- (IBAction)goBack:(id)sender
  {
      [UIView animateWithDuration:TRANSITION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  
          [self.keyFigureTableView setAlpha:0];
          self.keyFigureTableViewVerticalConstraint.constant = 700;
          [self.allFavoritesSegmentedControl setAlpha:0];
          [self.view layoutIfNeeded];
      } completion:^(BOOL finished) {
          [self.navigationController popViewControllerAnimated:NO]; // Pop viewController without animating it
      }];
  }
  </code></pre>
  
  <h2>Edit:</h2>
  
  <p>Most of the memory allocation takes place when pushing a view controller, even if it has already been displayed before. I.e. going from </p>
  
  <p>A -> B -> C</p>
  
  <p>B &lt;- C</p>
  
  <p>B -> C</p>
  
  <p>where "->" = push and "&lt;-" = pop, each "->" allocates more memory and "&lt;-" never releases any.</p>
  
  <h2>Further details</h2>
  
  <p>I have <strong>no zombies</strong> and <strong>no leaks</strong> according to Instruments. Static analysis also gives nothing. My app just keeps allocating memory until it finally crashes.</p>
  
  <p>Around 70 % of my memory allocation happens in the following call stack, which has nothing to do with my code (inverted call tree):</p>
  
  <p><img src="https://i.stack.imgur.com/MJsfj.png" alt="enter image description here"></p>