我已经构建了一个几乎已经完成的iOS应用程序,但是,我最近经历过它由于"内存压力"而崩溃.所以我开始在Instruments中分析内存分配,当然,应用程序确实使用了相当多的内存,而且在使用过程中似乎只会增加.
但是,对于仪器内存分配来说相对较新,我无法破译52%的分配,如下面的屏幕截图所示:
它显然与Core Animation有关,但究竟对我来说难以确定,所以我认为一些聪明的头脑可能知道答案.
我的应用程序使用自定义segue,在视图控制器之间移动时,正在进行大量动画.这是一个例子:
@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
每当要弹出一个视图控制器时,我只是做相反的事情:
- (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 }]; }
大多数内存分配在推送视图控制器时发生,即使它之前已经显示过.我会从
A - > B - > C.
B < - C.
B - > C.
其中" - >"= push和"< - "= pop,每个" - >"分配更多内存,"< - "永不释放任何内存.
根据乐器,我没有僵尸也没有泄漏.静态分析也没有给出什么.我的应用程序只是不断分配内存,直到它最终崩溃.
大约70%的内存分配发生在以下调用堆栈中,这与我的代码(反向调用树)无关: