iOS 8 UIWebView内存管理 [英] iOS 8 UIWebView memory management

查看:109
本文介绍了iOS 8 UIWebView内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分层应用程序(NavigationController),在根控制器的角色中有一个简单的表视图。每次选择任何单元格时,都会有一个新视图,其中包含有关详细信息的一些信息(由DetailViewController管理)。在DetailViewController视图的一侧,我有一个UIWebView来显示来自Internet的数据。但是当我查看内存使用情况时会出现问题。 DetailViewController的每个新打开视图及其内容通过UiWebView从Web加载,使用系数带来更多内存。当我回到根控制器的视图时,我想卸载所有内容并释放为该数据分配的内存。

I have a hierarchical application (NavigationController) with a simple table view in a role of the root controller. Each time you select any cell, you have got a new View with some information about the details (managed by DetailViewController). On the side of DetailViewController's view I have a UIWebView to display data from Internet. But the problem happens when I look at memory usage. Each new opened view of DetailViewController with its content loaded from the web via UiWebView brings more memory using coefficient. I'd like to unload everything and release memory allocated for that data when I get back to my root controller's view.

如何管理该问题?我试着 stopLoading: UIWebView的实例方法来保存一些内存,但它也没有解决问题。除了我不完全理解ARC的工作过程,我无法确保自己是否通常在每个新打开的详细信息视图中使用系数增加内存。

How do I manage that issue? I tried to stopLoading: UIWebView's instance method to save some memory, but it doesn't solve the problem either. As well as I don't completely understand ARC process of work, I can't ensure myself if it is normally to have an increased memory using coefficient with each new opened detail view.

提前谢谢!

推荐答案

要注意的几点事项。 UIWebView 泄漏。它至少从iOS4开始泄漏。随着每个大页面加载,它似乎增长,并且当 UIWebView 对象本身被释放时,内存不能完全补充。无论这个内存是保留在缓存中还是在真正需要时释放,或者是泄漏,我都无法正确回答。这种情况多年来有所改善,但仍然可以使用Instruments的内存分配图表来看。

A few things to note. UIWebView "leaks". It has been leaking since at least iOS4. With every large page load, it seems to grow, and the memory is not replenished fully right when the UIWebView object itself is released. Whether this memory remains in a cache and is released when really needed, or a leak, I have not been able to answer correctly. This has somewhat improved over the years, but still can be seen using Instruments' memory allocation graph.

让我们从基础开始。在开始更改设计之前,请尝试使用Instruments查看究竟是什么泄漏。您还可以对视图进行子类化并查看控制器,实现其dealloc方法并确保在预期时正确释放它们。很多时候,特别是当涉及块时,人们会创建保留周期,这会导致巨大的内存泄漏。首先执行此操作。

Let's start at the basics. Before you start making changes to your design, try to use Instruments to see what exactly is leaking. You can also subclass your views and view controllers, implement their dealloc methods and ensure that they are being released correctly when expected. Many times, especially when blocks are involved, people create retain cycles, that cause huge memory leaks. Do this first.

以下是我使用WebKit时的一些建议:

Here are a few recommendations from my experience working with WebKit:


  • 尽可能重用网页视图。如果可以,请使用相同的对象,然后将其添加为视图控制器视图的子视图。

  • 我们注意到,我们可以从Web视图中消耗的最多是在打开空白页面之前发布 UIWebView 对象。

  • iOS8支持使用WebKit的新模型: WKWebView (WebKit2)。在此模型中,Web内容被管理并从进程中抽出,并且内存在该进程中泄露。在需要时,操作系统将终止这些WebKit进程,从而允许您的应用程序运行。你可以尝试这个,看看你是否有改进。

  • Reuse the web view as much as possible. If you can, use the same object and just add it as subview of the view controllers' views.
  • We noticed that the most we could drain from the web view was opening a blank page before releasing the UIWebView object.
  • iOS8 supports a new model with working with WebKit: WKWebView (WebKit2). In this model, the web content is managed and drawn off-process, and the memory is "leaked" in that process. When needed, the OS will kill these WebKit process, thus allowing your app to run. You can try this and see if you have improvements.

这篇关于iOS 8 UIWebView内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆