简体   繁体   English

如何处理iPhone上的低内存警告?

[英]How to deal with low memory warnings on the iPhone?

I'm having problems with my application receiving low memory warnings while the user is deep within a navigation controller stack of views. 当用户深入导航控制器堆栈视图时,我的应用程序接收到低内存警告时遇到问题。 After the user browses through a bunch of hierarchical options in subsequent UITableViews, he can open a PDF document in a UIWebView (in a different view controller). 在用户浏览后续UITableViews中的一堆分层选项后,他可以在UIWebView中打开PDF文档(在不同的视图控制器中)。

Everything works fine, the PDF loads and the user can flip through the pages. 一切正常,PDF加载,用户可以浏览页面。 However, when the document is a bit large, or has several pages, and the user taps on the "Back" button in the navigation controller, he app crashes as the previous view controller in the navigation controller stack has been deallocated. 但是,当文档有点大,或者有多个页面,并且用户点击导航控制器中的“后退”按钮时,他的应用程序崩溃,因为导航控制器堆栈中的前一个视图控制器已被解除分配。

After searching around for ways to deal with low memory warnings, and dealing with this type of problem, I found several posts that just advise people to release the objects that can be released, and then lazy-load them later on when the user tries to load a view that has been deallocated. 在寻找处理低内存警告的方法以及处理这类问题之后,我发现了一些帖子,建议人们释放可以释放的对象,然后在用户尝试时稍后加载它们。加载已解除分配的视图。 One of these posts is Craig Hockenberry's Dealing with memory loss: the cleanup post. 其中一篇文章是Craig Hockenberry的“ 处理记忆丧失:清理帖子”。

While that's a bit helpful, it doesn't give me much information to work from. 虽然这有点帮助,但它并没有给我很多信息。

Can someone provide a simple guide on how to deal with low memory warnings, and how to implement "lazy loading" of objects? 有人可以提供一个关于如何处理低内存警告的简单指南,以及如何实现对象的“延迟加载”?

When memory is low, the system sends out a series of memory-related messages. 当内存不足时,系统会发出一系列与内存相关的消息。 Any instantiated view controllers will get the -didReceiveMemoryWarning message. 任何实例化的视图控制器都将获得-didReceiveMemoryWarning消息。 If you don't implement this, the default action (assuming the view controller is not front-most) is to release the controller's view member. 如果不实现此操作,则默认操作(假设视图控制器不是最前面的)是释放控制器的视图成员。 If you've got hooks into it, or perhaps into its subviews, that could cause issues when your controller gets returned to the top of the stack. 如果您有钩子,或者可能进入其子视图,那么当您的控制器返回到堆栈顶部时,这可能会导致问题。

Your first task is to figure out what exactly is causing the problem. 您的首要任务是弄清楚导致问题的确切原因。 Which object is being deallocated? 哪个对象被解除分配? Usually, fixing this is simply a matter of making sure the object is retained properly. 通常,修复此问题只是确保正确保留对象。

I suggest you use NSZombiesEnabled to try and track down the object you're having trouble with. 我建议你使用NSZombiesEnabled来尝试追踪你遇到问题的对象。 Once you have that, you can make sure you're retaining it properly. 一旦你有了,你可以确保你正确地保留它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM