【问题标题】:UIPageViewController doesn't release memory when page is flipped翻页时 UIPageViewController 不释放内存
【发布时间】:2015-10-27 01:31:14
【问题描述】:

我正在做一个电子书项目,我也在使用 ARC,但是,当我在模拟器中翻页时,我的内存迅速增长,当我完成翻页时,它已接近 200MB。似乎内存没有被释放,但这不是 ARC 的用途吗?我删除了我的 NSTimer 和所有代表,看看这是否是罪魁祸首,但无济于事。好像我在某个地方有一个保留周期,但我似乎找不到它。我也没有使用积木......有什么想法吗?

编辑:这是一个 UIPageViewController 应用程序

我替换了以下代码

        - (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard {
            // Return the data view controller for the given index.
            if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
                return nil;
            }
            // Create a new view controller and pass suitable data.
            DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"DataViewController"];
        with
        - (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard {
            // Return the data view controller for the given index.
            if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
                return nil;
            }
            // Create a new view controller and pass suitable data.
            DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:self.pageData[index]];

其中 self.pageData[index] 是情节提要中视图控制器的 ID。我希望 pageViewController 在请求下一个 viewController 时释放这个 viewController

【问题讨论】:

  • 使用“仪器”实用程序查看分配。它可以向您显示存在哪些对象以及它们是在哪里创建的。
  • 关于您对 ARC 的评论的快速说明...使用 ARC,编译器将为您处理引用计数并进行内存管理调用...但是如果您是 (无意中)抓住对象,它不会摆脱它们;)我猜UIPageViewController中的页面很大,这就是内存的积累。就像 Phillip 所说,尝试使用 Instruments 进行调查,或者,只需查看您的代码。您可能会发现错误...(或在您的问题中发布一些错误)。
  • 模板和我的应用程序之间的唯一区别是每个页面都是不同的视图控制器,而不是重复使用相同的视图控制器......我也将代码添加到我的问题中

标签: ios objective-c memory memory-management automatic-ref-counting


【解决方案1】:

经过数小时的研究,UIPageViewController 似乎有意保留对所有视图控制器的永久引用并且从未释放它们(即使使用 ARC) 解决方案是在我切换页面时手动调用 [self setView:nil]。

【讨论】:

  • 嗨,我遇到了控制器保留页面的类似问题,这是一个问题。您能否详细说明“UIPageViewController 有意保留永久引用”。这种行为是否记录在某处?我认为这是一个错误......
  • 我目前遇到了同样的问题。您能否详细说明您在何时何地调用 [self setView:nil];?
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 2014-08-16
  • 2017-06-29
  • 1970-01-01
  • 2018-01-31
  • 2011-10-28
相关资源
最近更新 更多