iOS - ModalViewControllers 的 ARC/内存管理问题
我正在编写一个应用程序(在 iOS 5 中使用 ARC!),它在自定义 UIViewController 中呈现数百个对象,我已经编写了用户可以滚动并选择的对象,每个对象都呈现为用户可以触摸的缩略图。
这些对象中的每一个都与一个特殊的 UIViewController
子类关联,该子类处理与该对象关联的信息的自定义表示。例如,它可能是可以缩放和平移的图像,或者只是一些需要格式化的文本。
现在,我的每个图像对象都有一个与之关联的大图像。使用 Instruments 应用程序来分析我的代码并运行 Activity Monitor,我发现当加载自定义 UIViewController
子类并显示图像时,每个代码大约占用 5-10MB。对于一些图像来说这很好,但最终我的应用程序占用了太多内存并崩溃了。
我已经在子类的 -viewDidUnload
方法中编写了显然告诉 ARC 释放此内存所需的所有内容,但在发出警告之前不会释放任何内存,这通常发生在应用程序运行时崩溃。有几次,我注意到,如果我足够接近阈值但不超过它,我之前查看过的 UIViewController 子类最终会从内存中清除,尽管-viewDidUnload
方法显然没有被调用(相反,-didReceiveMemoryWarning
消息被发送到我的自定义 UIViewControllers
)。然而,大多数情况下,我的应用程序会因为内存不足而崩溃。
所以我的核心问题是,我应该假设一个对象会尽快被 ARC 处理掉,还是总是等到空间变得紧张?我想要的行为是立即刷新自定义视图控制器及其数据,这样内存就不会成为问题。
我不相信对象对我的自定义视图控制器有任何强引用,并且我使用以下代码在主视图控制器中实例化它们:
[self presentViewController:[cObj grabModalViewController]
animated:YES completion:nil];
其中 cObj
是包含对象信息的自定义类。 grabModalViewController 只是实例化一个正确类型的对象并返回指针,因此大概该方法完成后,对该对象的本地引用就应该被丢弃。
因此,我希望当我稍后调用
[self dismissModalViewControllerAnimated:YES];
仅通过 PresentedViewController 指向主视图控制器的自定义视图控制器时,应该从内存中刷新,但这不会发生。
这可能是因为我的自定义视图控制器对象中有强引用吗?
本质上,我想要完成的是通过预先更好地管理事情来避免尽可能多的内存警告,但这也许不是正确的态度。
我将不胜感激任何建议,并且很乐意发布任何有帮助的代码。
I'm writing an app (in iOS 5 using ARC!) that presents several hundred objects within a custom UIViewController
I've written that a user may scroll through and select, each of which is presented as a thumbnail image that the user may touch.
Each of these objects has associated with it a special UIViewController
subclass that handles the custom presentation of the information associated with that object. For example, it might be an image that can be zoomed and panned, or simply some text that needs to be formatted.
Now, each of my image objects has a large image associated with it. Using the Instruments app to profile my code and running Activity Monitor, I see that these take up roughly 5-10MB each when the custom UIViewController
subclass loads and the image is displayed. This is fine for a few images, but eventually my app takes up too much memory and crashes.
I have written in my subclass's -viewDidUnload
method apparently all of the things necessary to tell ARC to free up this memory, but no memory is being freed until warnings are issued, which typically occurs when the app is about to crash. A couple of times, I've noticed that if I get close enough to the threshold but don't go over it, the UIViewController
subclasses that I've previously viewed are eventually flushed from memory, although the -viewDidUnload
method is not apparently called (rather, the -didReceiveMemoryWarning
message is sent to my custom UIViewControllers
). However, most often, my app crashes as it runs out of memory.
So my core question is, should I assume that an object will be disposed of by ARC as soon as possible, or does it always wait until space becomes tight? The behavior I want is for the custom view controller and its data to be flushed right away, so that memory never becomes an issue.
I do not believe there are any strong references from objects to my custom view controllers, and I instantiate them in my primary view controller using this code:
[self presentViewController:[cObj grabModalViewController]
animated:YES completion:nil];
where cObj
is a custom class with the object's information. grabModalViewController
simply instantiates an object of the correct type and returns the pointer, so presumably the local reference to the object should be trashed as soon as the method completes.
As a result, I would expect that, when I later call
[self dismissModalViewControllerAnimated:YES];
the custom view controller that was only pointed to be the primary view controller via presentedViewController
should be flushed from memory, but this does not happen.
Could this be because I have strong references within my custom view controller object?
Essentially what I'm trying to accomplish is to avoid as many memory warnings as possible by managing things better up front, but perhaps this is not the right attitude.
I'd appreciate any suggestions and I'm happy to post any and all code that would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ARC是一种编译时技术。它对您的运行时内存堆一无所知。因此,它不能等到空间紧张才释放物品。您问题的具体答案是,一旦您不再需要任何内容,ARC 就会立即释放它。
ARC is a compile time technology. It knows nothing about your runtime memory heap. Hence, it cannot wait to release items until space becomes tight. A specific answer to your question is that ARC will release anything as soon as you no longer need it.