iOS4 - 快速上下文切换

发布于 2024-09-08 00:51:13 字数 310 浏览 5 评论 0原文

当应用程序进入后台运行状态时,有多少脏内存使用量就可以了。在苹果视频中提到应尽可能减少脏内存。

但在我的应用程序中,我使用导航控制器来推送和弹出视图。从大约 20 个不同的页面移动后,脏内存使用量达到 30 MB 左右。

另外,在“dismissModalViewControllerAnimated”和“popViewControllerAnimated”上,不会调用 dealloc。

我有两个疑问:

  1. 多少脏内存可以上线?
  2. 支持后退按钮的导航控制器的替代方案是什么?

提前致谢。

When application enters in background running state, how much dirty memory usages is good to go. In apple video it's mentioned that the dirty memory should be reduced as much as we can.

But in my App, I am using navigation controller to push and pop views. After moving from about 20 different pages, the dirty memory usages reaches 30 MB or so.

Also on "dismissModalViewControllerAnimated" and "popViewControllerAnimated", dealloc is not called.

I have two doubts:

  1. With how much dirty memory is acceptable to go live?
  2. What is the alternate of navigation controller to support back button?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃素的狼 2024-09-15 00:51:13

如果未调用 dealloc,您可能仍会保留 UIViewController。

也许您在这些 UIViewController 中设置委托或其他类来保留并引用备份树(循环引用)。

调试此问题的一种方法是在 UIViewController 中重载保留和释放,并设置断点并记录保留计数。

这是我留下的一个神奇的片段,当我不明白为什么我仍然保留某些东西时,它对我有很大帮助。

- (id)retain
{
    NSLog(@"retain \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    return [super retain];
}
- (void)release
{
    NSLog(@"release \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    [super release];
}
- (id)autorelease
{
    NSLog(@"autorelease \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    return [super autorelease];
}

__PRETTY_FUNCTION__ 是 CLang 中的一个特殊隐藏宏,它以 char 数组的形式提供漂亮的 Objective-C 函数名称。

You might still have your UIViewControllers still retained if dealloc isn't being called.

Perhaps are you setting delegates or other classes in these UIViewControllers that retained and referenced back up the tree (circular references).

A way you can debug this is to overload retain and release in your UIViewController and set a break point and log the retainCount.

Here is a magic snippet I leave running around that helps me a ton when I can't figure out why I'm still retaining something.

- (id)retain
{
    NSLog(@"retain \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    return [super retain];
}
- (void)release
{
    NSLog(@"release \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    [super release];
}
- (id)autorelease
{
    NSLog(@"autorelease \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    return [super autorelease];
}

__PRETTY_FUNCTION__ is a special hidden macro in CLang that gives a pretty Objective-C function name as a char array.

只怪假的太真实 2024-09-15 00:51:13
  1. 当 iOS 开始内存不足时,它会尝试终止使用最多内存的后台进程。因此,虽然没有绝对的好数字,但尽量减少使用的内存量是个好主意。将其保留在 30Mb 就等于保证您的应用程序将被终止
  2. 除非您想更改 UI,否则无需使用 UINavigationController 之外的任何其他内容来处理后退按钮。我认为这里的问题是,如果在弹出或关闭时未调用dealloc,则会出现内存泄漏

几乎所有视图控制器都具有有效缓存的数据,并且可以在应用程序返回时重新生成前景。想想当应用程序运行时收到内存警告时释放的数据。 (您正在响应内存警告,对吗?)这是您进入后台时应该释放的东西。

  1. When an iOS starts running out of memory it tries to kill the background processes that are using the most memory. So while there's no absolute good number, minimising how much memory you use is a good idea. Leaving it at 30Mb is tantamount to guaranteeing that your app will be killed
  2. Unless you want to change your UI there is no need to use anything other that a UINavigationController to deal with your back button. I think the problem here is that if dealloc is not called on a pop or dismiss, you have a memory leak

Almost all view controllers have data that is effectively cached and can be regenerated when the app returns to the foreground. Think of the data that you release when you get a memory warning when the app is running. (You are responding to memory warnings, right?) It's that stuff that should be released when you go into the background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文