gpt4 book ai didi

ios - 内存堆积,在ViewController之间切换,什么是ok的?

转载 作者:行者123 更新时间:2023-11-29 02:41:26 25 4
gpt4 key购买 nike

预先感谢您的宝贵时间!

适用于 iOS 7+ 应用程序的 Xcode 版本 5.1.1,启用了 ARC。

我正在研究在不使用 UINavigationController 或 UITabBarController 的情况下在多个 UIViewController 之间切换的最佳模式;最好是关于内存分配和释放。我远离导航和标签栏,因为它们使用自己的导航对象并占用屏幕上的空间。

为了测试我做了两个测试项目。有两个 UIViewControllers、两个 UIButtons 和两个 UILabels...

1) 使用通过按住 CTRL 键将 UIButton 拖动到下一个 UIViewController 并选择“模态”来设置的触发转场。作为返回,UIButton 的 Touch Up Inside IBAction 用于关闭第二个 UIViewController,即

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

2) 使用这个 SO 帖子中描述的过渡 View Controller :Animate change of view controllers without using navigation controller stack, subviews or modal controllers? (感谢所有贡献者)。要转到第二个 UIViewController,使用 UIButton 的 Touch Up Inside IBAction,即

- (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];
}

返回,UIButton 的 Touch Up Inside IBAction 用于第二个 UIViewController,即

- (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];
}

项目中定义了一个TransitionController UIViewController类型的类。

--

我知道有无数种方法可以做到这一点,例如展开 UIViewController。但这是我最熟悉的两种方式。

Instruments Allocations 分析用于使用实际的 iPhone 4(而非模拟器)制作以下内存分配和释放配置文件图。

1) 从第一个 UIViewController 到第二个 UIViewController 进行 12 次转换后,净变化为 11.96kB。

2) 在从第一个 UIViewController 到第二个 UIViewController 的 6 次转换之后,注意到净变化为 54.66kB(在发布时意识到只有 6 个,因为在切换到第二个 UIViewController 之后,为第一个 UIViewController 分配的内存被释放) .

enter image description here

--

我对这些图的解释是,这两种方法都工作得相当好,但是它们都不会导致净零内存分配变化。

(1) 表明第一个 UIViewController 始终被分配,第二个 UIViewController 被分配和解除。

(2) 显示为第一个或第二个 UIViewController 之间的每次转换分配过渡 UIViewController。然而,无论何时分配了过渡 UIViewController 都会被取消,即内存配置文件看起来像一个步骤,然后立即下降,而不是像 (1) 中那样,步骤一直持续到第二个 UIViewController 被取消。

测试是在实际的 iPhone 上进行的,因此绘图上未捕获内存警告。但是,在模拟器上,当触发内存警告时,会释放一些额外的内存,但它永远不会回到基线。

--

我的问题具体...

问题1)这些结果可以接受吗?

问题2)在现实世界中无法实现净零内存更改吗?如果是这样,那么对于每个 UIViewController 有大量对象的实际应用程序来说,可接受的内存泄漏量是多少? 100kB、200kB...?

Q3)我应该使用什么设计模式?还有其他人吗?效率更高?

再次感谢大家。

最佳答案

一个有趣的问题。以下是一些想法:

1) 看起来您正在使用 Storyboard。 Storyboard是 iOS 生态系统中的怪兽。它们类似于 nib,但似乎具有超出常规的额外缓存逻辑。我会尝试在不使用 Storyboard 的情况下重现您的结果,看看您会得到什么。

2) 带有转换 Controller 的第二个版本会占用更多内存,因为您现在有一个额外的 View Controller 在运行,而不是原来的两个,所以这将占用额外的内存。

总的来说,随着操作系统构建其缓存以及运行时为您缓存内容,应用程序中的内存会增加一些。根据应用程序的大小,您可能有 50k 或 5000k 的内存增长。在这种特殊情况下,我认为 15-30k 的增长是可以接受的。

关于ios - 内存堆积,在ViewController之间切换,什么是ok的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25734816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com