简体   繁体   English

内存累积,在UIViewControllers之间切换,可以吗?

[英]Memory Accumilation, switching between UIViewControllers, what is ok?

Thanks in advance for your time! 在此先感谢您的时间!

Xcode Version 5.1.1 for iOS 7+ applications, ARC enabled. 适用于iOS 7+应用程序的Xcode版本5.1.1,已启用ARC。

I am researching the best pattern for switching between multiple UIViewControllers without using a UINavigationController or a UITabBarController; 我正在研究在不使用UINavigationController或UITabBarController的情况下在多个UIViewController之间切换的最佳模式; best being with respect to memory allocation and release. 关于内存分配和释放最好。 I am staying away from navigation and tab bar because they use their own navigation objects and take up real estate on the screen. 我远离导航和标签栏,因为它们使用自己的导航对象并占用了屏幕上的空间。

For the sake of testing I made two test projects. 为了测试,我做了两个测试项目。 With two UIViewControllers, two UIButtons and two UILabels... 有两个UIViewControllers,两个UIButton和两个UILabel ...

1) Uses a triggered segue that was set up by CTRL-dragging a UIButton to the next UIViewController and selecting "modal". 1)使用触发的序列,该序列是通过CTRL拖动UIButton到下一个UIViewController并选择“ modal”来设置的。 And to return, a UIButton's Touch Up Inside IBAction is used to dismiss the second UIViewController, ie 作为返回,UIButton的IBAction内部的Touch Up用于关闭第二个UIViewController,即

- (IBAction)pressedButtonGoToTwo:(UIButton *)sender
{
    [self dismissViewControllerAnimated:true completion:nil];
}

2) Uses a transitional view controller described in this SO post: Animate change of view controllers without using navigation controller stack, subviews or modal controllers? 2)使用本SO文章中描述的过渡视图控制器:在不使用导航控制器堆栈,子视图或模式控制器的情况下对视图控制器进行动画更改? (thanks to all who contributed). (感谢所有贡献者)。 To go to the second UIViewController a UIButton's Touch Up Inside IBAction is used, ie 要转到第二个UIViewController,使用UIButton的IBAction内部的Touch Up,即

- (IBAction)pressedButtonGoToOne:(UIButton *)sender
   {
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main"    bundle:nil];
        TBAViewController *vc2 = [mainStoryBoard instantiateViewControllerWithIdentifier:@"TBAViewController2"];
        TBAAppDelegate *tbaAppDelegate = [UIApplication sharedApplication].delegate;
        [tbaAppDelegate.transitionController transitionToViewController:vc2 withOptions:UIViewAnimationOptionTransitionFlipFromBottom];
   }

And to return, a UIButton's Touch Up Inside IBAction is used on the second UIViewController, ie 作为返回,在第二个UIViewController上使用UIButton的Touch Up Inside IBAction,即

- (IBAction)pressedButtonGoToOne:(UIButton *)sender
   {
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main"    bundle:nil];
        TBAViewController2 *vc1 = [mainStoryBoard instantiateViewControllerWithIdentifier:@"TBAViewController"];
        TBAAppDelegate *tbaAppDelegate = [UIApplication sharedApplication].delegate;
        [tbaAppDelegate.transitionController transitionToViewController:vc1 withOptions:UIViewAnimationOptionTransitionFlipFromBottom];
   }

A TransitionController UIViewController type class is defined in the project. 在项目中定义了TransitionController UIViewController类型类。

-- -

I understand there are countless ways of doing this, eg unwind UIViewController. 我知道有无数种方法可以做到这一点,例如放松UIViewController。 But these are two ways I am most familiar with. 但是,这是我最熟悉的两种方式。

Instruments Allocations analysis was used to make the following plots of memory allocation and release profiles using an actual iPhone 4 (not simulator). 仪器分配分析用于使用实际的iPhone 4(而非模拟器)绘制以下内存分配和释放配置文件图。

1) The net change of 11.96kB was noted after 12 transitions from the first to the second UIViewController. 1)从第一个UIViewController到第二个UIViewController进行了12次转换后,记录了11.96kB的净变化。

2) The net change of 54.66kB was noted after 6 transitions from the first to the second UIViewController (realized there were only 6 at time of post, because after switching to the second UIViewController the allocated memory for the first is released). 2)在从第一个UIViewController到第二个UIViewController进行了6次转换后,注意到净变化为54.66kB(意识到在发布时只有6个,因为在切换到第二个UIViewController之后,释放了为第一个UIViewController分配的内存)。

在此处输入图片说明

-- -

My interpretation of the plots is that both approaches work reasonably well, however none of them result in a net zero memory allocation change. 我对这些图的解释是,这两种方法都可以很好地工作,但是它们都不导致零内存分配净变化。

(1) shows that the first UIViewController is always allocated and the second UIViewController is allocated and dismissed. (1)显示了始终分配第一个UIViewController并分配和关闭第二个UIViewController。

(2) shows that the transitional UIViewController is allocated for each transition between either the first or second UIViewControllers. (2)显示为第一个或第二个UIViewController之间的每个过渡分配了过渡UIViewController。 However, whenever either are allocated the transitional UIViewController is dismissed, ie the memory profile looks like a step up and immediately down, not like in (1), where the step up lasts until the second UIViewController is dismissed. 但是,无论何时分配了过渡UIViewController,都将被关闭,即,内存配置文件看起来像是逐步升高然后立即降低,而不像(1)中那样,升高持续到第二个UIViewController被消除为止。

The tests were performed on an actual iPhone, so a Memory Warning was not captured on the plots. 测试是在实际的iPhone上执行的,因此未在图中捕获“内存警告”。 However, on a simulator when the Memory Warning is triggered, some additional memory is released but it never gets back to the baseline. 但是,在模拟器上,当“内存警告”被触发时,会释放一些额外的内存,但它永远不会回到基线。

-- -

My questions specifically... 我的问题特别是...

Q1) Are these results acceptable? Q1)这些结果可以接受吗?

Q2) Is a net zero memory change not achievable in the real world? Q2)在现实世界中是否无法实现零存储净更改? If so, then what is an acceptable amount of memory leaked for an actual app with lots of objects per UIViewController? 如果是这样,那么对于每个UIViewController具有很多对象的实际应用程序,泄漏的可接受内存量是多少? 100kB, 200kB...? 100kB,200kB ...?

Q3) What design pattern should I be using? Q3)我应该使用哪种设计模式? Are there others? 还有其他吗? More efficient? 更高效?

Thanks again guys. 再次感谢你们。

An intriguing problem. 一个有趣的问题。 Here are some thoughts: 这里有一些想法:

1) Looks like you're using a storyboard. 1)看起来您正在使用情节提要。 Storyboards are strange beasts in the iOS ecosystem. 故事板是iOS生态系统中的怪兽。 They are like nibs, but appear to have additional caching logic in place beyond the norm. 它们就像笔尖,但似乎在规范之外还具有其他缓存逻辑。 I would attempt to reproduce your results without using a storyboard and see what you get. 我会尝试在不使用情节提要的情况下重现您的结果,然后看看您得到了什么。

2) The second version with the transition controller makes sense you'd take up more memory because you now have an additional view controller in play beyond the original two, so that will take up extra memory. 2)带有过渡控制器的第二个版本使您可以占用更多的内存,因为您现在可以在原有的视图控制器的基础上增加一个视图控制器,从而占用更多的内存。

Overall you're going to have some memory growth in the application as the OS builds out its caches, along with the runtime caching things for you. 总体而言,随着操作系统构建其缓存以及运行时缓存内容,您将在应用程序中增加一些内存。 Depending on the size of the application, you might have 50k or 5000k memory growth. 根据应用程序的大小,您可能会增加50k或5000k的内存。 In this particular scenario, I would see 15-30k worth of growth being acceptable. 在这种特定情况下,我认为可以接受15-30k的增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM