防止 UIWebView 分配过多内存
我的 UIWebView 有问题。我正在使用这个组件在 iPad/iPhone 上加载普通网页。 UIWebView 呈现在 modalViewController 中。有些页面非常占用内存,有些页面还包含 Flash 内容。
问题是,有时在某些页面上我会收到内存警告,
Received memory warning. Level=1
当发生这种情况并且当我关闭此 modalViewController (已在 webView 中加载页面)时,前一个视图会自动重新加载 - 方法 viewDidLoad 再次被触发。
我怀疑 UIWebView 中的某些页面消耗了大量内存,并且应用程序释放了其视图的内存,但不会崩溃。
有没有办法限制 webView 的内存消耗或者有其他方法来避免这个内存警告?
谢谢!
I have a problem with UIWebView. I am using this component for loading normal web pages on iPad/iPhone. UIWebView is presented in modalViewController. Some pages are very memory hungry and some also have Flash content.
The problem is that sometimes on some pages I get memory warning
Received memory warning. Level=1
When this happens and when I close this modalViewController (which has page loaded in webView) than the previous view reloads itself automacially - method viewDidLoad is fired again.
I suspect that some pages in UIWebView consume to much memory and than application release memory of its views, but does not crash.
Is there a way to limit memory consumption of webView or is there any other way to avoid this memory warning?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您几乎无法控制 UIWebView 对象的内存使用情况。您对整个系统内存使用情况也几乎没有控制权。所以没有办法避免内存警告。 iOS 希望您的应用程序在收到内存警告时能够正常运行,因此您的
viewDidLoad
方法应编写为处理内存警告后的重新运行。要限制
UIWebView
的内存使用,您唯一可以做的就是让它查看简单的网页。从你的问题来看,这不是一个选择。编辑:
viewDidUnload
在内存不足的情况下被调用。此方法预计会释放 viewDidLoad 方法中易于重新创建的任何内容。当此视图返回到屏幕时,不要释放要向用户显示的状态信息。然后在您的viewDidLoad
方法中检查您创建的所有对象。如果它们为 nil,则需要初始化它们,否则这不是第一次调用 viewDidLoad 方法,并且您不应该再次初始化对象。换句话说:
viewDidUnload
处理低内存情况,viewDidLoad
不应该假设它运行一次。You have very little control of the memory usage of
UIWebView
objects. You also have very little control of the overall system memory usage. So there is no way to avoid memory warnings. iOS expects your apps to behave properly when receiving memory warnings, so yourviewDidLoad
method should be written to handle rerunning after a memory warning.The only things you can do to limit the memory usage of a
UIWebView
is to have it view simple web pages. It looks like from your question, that's not an option.Edit:
viewDidUnload
is called during low-memory conditions. This method is expected to free up anything that's easy to recreate in the viewDidLoad method. Don't release the state information you want to show the user when this view is returned to the screen. Then in yourviewDidLoad
method check all the objects you create. If they arenil
, they need to be initialized, otherwise this isn't the first time yourviewDidLoad
method was called, and you shouldn't initialize your objects agin.In other words:
viewDidUnload
handles low-memory situations andviewDidLoad
should not assume it is run once.