0

我有一个带有滚动视图的 uiviewcontroller 和一些 uiswitches(在一个空白测试项目中检查这个问题)。当我第一次按下它时,它需要三分之一兆的内存,而当我卸载它时,我并没有找回那些内存。后续加载不会占用更多内存,但我对浪费的半兆感到不满意,因为我想添加更多视图控制器,这是浪费的内存,因为它们可能在使用应用程序的早期阶段使用一次,然后不用于剩下的几个小时可能会使用该应用程序。

那么发生了什么,操作系统是否正在缓存视图控制器(或其控件)?如果我把它剥离成一个视图控制器,它只会占用大约 20k 的卸载后。添加 5 个 uiswitches 和属性,没有任何变化,但是当我将它们连接到它们的属性时,它会上升到 300k,另外 5 个控件会上升到大约 550k。

我在 viewDidUnload 中将它们设置为 nil,并在 dealloc 中取消和释放它们,并且在卸载时调用了 dealloc。但尽管如此,我并没有恢复记忆。它们被声明为属性(不是 ivars,依赖于自动 ivar 创建)并被 authsynthesised,除了在释放的 dealloc 中之外,我没有直接访问 ivars。在分析或仪器中没有报告内存泄漏。就在这个“第一次内存突袭”之后,viewcontrollers 对我的用户 ram 的胃口似乎已经过时了。

在仪器中,堆栈跟踪中没有提到我的任何代码(除了 main 之外),并且大多数游荡垃圾似乎来自 UIKit 和类似的。

正如我所说,我能想到的唯一怀疑是 ios 正在努力变得聪明,并想象我将在某个时候重用这些东西,但这完全是浪费内存(在我的最终应用程序中,这将占用至少 10 兆以这个速度总计)。如果这是苹果的选择,有没有办法覆盖它并强制将内存归还给我?

在此先感谢您的任何指点,这让我发疯了。

4

1 回答 1

0

If you are using iOS6, then viewDidUnload is not being called, you need to release your memory elsewhere.

Rather than bang your head and guess at what is holding on to the memory run your app in instruments using the allocations Instrument.

Now, take a snapshot of your memory before you show your view controller, and then take another snapshow with the view view controller loaded, then take another snapshot after you unload the controller.

YOu will now be able to see what objects still persist in memory between the snapshots, and this should help you see what objects are in memory.

于 2013-06-20T11:44:05.047 回答