UIWebView 未使用 ARC 释放所有活动字节

2024-01-06

我目前正在 iOS 5.1 中构建一个使用 ARC 的导航控制器应用程序。我经常需要显示网页,我制作了一个 Web 查看器,它只是一个 UIWebView,两侧有一些自定义内容。当用户看完页面后,他们点击后退按钮,这将释放与自定义 Web 查看器相关的所有内存。我的问题是,当按下后退按钮时,所有内存似乎都没有被释放。我构建了一个玩具应用程序(在 github 上 https://github.com/bfruin/WebViewMemory)这只是几个按钮,每个按钮都有一个调用不同页面的第一响应者。

@implementation ViewController
-(IBAction)googlePressed:(id)sender
{
   CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.google.com"];
   [self.navigationController pushViewController:customWebView animated:NO];
 }
-(IBAction)ksbwPressed:(id)sender
{
   CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.ksbw.com/news/money/Yahoo-laying-off-2-000-workers-in-latest-purge/-/1850/10207484/-/oeyufvz/-/index.html"];
   [self.navigationController pushViewController:customWebView animated:NO];
}
-(IBAction)feedProxyPressed:(id)sender
{
   CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://feedproxy.google.com/~r/spaceheadlines/~3/kbL0jv9rbsg/15159-dallas-tornadoes-satellite-image.html"];
   [self.navigationController pushViewController:customWebView animated:NO];
}
-(IBAction)cnnPressed:(id)sender
{
   CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.cnn.com/2012/04/04/us/california-shooting/index.html?eref=rss_mostpopular"];
   [self.navigationController pushViewController:customWebView animated:NO];
}

CustomWebView 只是 IB 中链接到 UIWebView 属性的 UIWebView

@implementation CustomWebView

@synthesize webView, link;

- (id)initWithStringURL:(NSString *) stringURL
{
   if (self = [super init]) {
       link = stringURL;
   } 
   return self;
}


- (void)viewDidLoad
{
   [super viewDidLoad];
   NSURL *url = [NSURL URLWithString:link];
   NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
   [webView loadRequest:urlRequest];
}

- (void)viewDidUnload
{
   [super viewDidUnload];
}

我的问题是,一旦加载了所有内容,我就将堆基线设置为初始 ViewController。然后,我在加载页面后检查堆,然后返回 ViewController 并获得以下堆快照:

这表明,在每次单击按钮并返回 ViewController 后,即使 CustomWebView 都应该被释放,堆也会继续增长。

EDIT:

上述示例项目可以找到在 github 上 https://github.com/bfruin/WebViewMemory


我有同样的问题,我发现这个小魔法 http://www.codercowboy.com/code-uiwebview-memory-leak-prevention/

/*

 There are several theories and rumors about UIWebView memory leaks, and how
 to properly handle cleaning a UIWebView instance up before deallocation. This
 method implements several of those recommendations.

 #1: Various developers believe UIWebView may not properly throw away child
 objects & views without forcing the UIWebView to load empty content before
 dealloc.

 Source: http://stackoverflow.com/questions/648396/does-uiwebview-leak-memory

 */        
[webView loadHTMLString:@"" baseURL:nil];

/*

 #2: Others claim that UIWebView's will leak if they are loading content
 during dealloc.

 Source: http://stackoverflow.com/questions/6124020/uiwebview-leaking

 */
[webView stopLoading];

/*

 #3: Apple recommends setting the delegate to nil before deallocation:
 "Important: Before releasing an instance of UIWebView for which you have set
 a delegate, you must first set the UIWebView delegate property to nil before
 disposing of the UIWebView instance. This can be done, for example, in the
 dealloc method where you dispose of the UIWebView."

 Source: UIWebViewDelegate class reference    

 */
[webView setDelegate:nil];


/*

 #4: If you're creating multiple child views for any given view, and you're
 trying to deallocate an old child, that child is pointed to by the parent
 view, and won't actually deallocate until that parent view dissapears. This
 call below ensures that you are not creating many child views that will hang
 around until the parent view is deallocated.
 */

[webView removeFromSuperview];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UIWebView 未使用 ARC 释放所有活动字节 的相关文章

随机推荐

  • 自定义键盘中handleInputModeList的正确实现

    iOS 10 中添加了一个新的 API 用于显示用户可以切换到的其他键盘列表 与用户在系统键盘上长按地球仪时出现的列表相同 函数的声明如下 func handleInputModeList from view UIView with eve
  • 如何在 Python 中将折叠标量转储到 YAML(使用 ruamel?)

    我一直在 stackoverflow 上搜索 寻找一种使用 Python 以 YAML 格式转储折叠标量的方法 普通的answer https stackoverflow com a 35406862 3615411来自用户Anthon h
  • 无法远程启动WebLogic进行调试

    我使用以下选项设置远程 WebLogic 服务器进行调试 Xdebug Xnoagent Xrunjdwp transport dt socket address DEBUG PORT server y suspend n Djava co
  • SVN 到 GitHub 迁移

    我必须将多个目录从 SVN trunk 迁移到one单个 GitHub 存储库 我可以使用以下命令一次克隆单个目录 git svn clone https svn repo url 如何使用 svn trunk 中的单个命令克隆多个目录 进
  • 错误:无法找到类 R.java 没有这样的文件或目录

    当我尝试开始一个新项目时 我不断收到此消息 ERROR Unable to open class file C Users Levi Desktop Android workspace Droid1 gen com androidbook
  • rbenv:权限被拒绝

    我正在关注 Ryan 的 RailsCast Episode 339 我已经安装了 rbenv 并且可以运行ruby v 我退出了会话 当我尝试返回时 通过su deployer from root 我收到这个错误 home deploye
  • 如何在不停止管道的情况下在多个 rtsp 视频流之间切换 [无缝流媒体]

    我使用 5 个 ip 摄像机 每个摄像机为我提供 5 个 RTSP 流 我选择这些 RTSP 视频流中的任何一个 并将它们与我的麦克风 音频 RTSP 流源 混合并将其广播到我的 RTMP 服务器 我尝试进行无缝流传输 这意味着当相机到相机
  • Node.js 应用程序中出现“EACCES”错误

    今天我通过卸载旧版本来更新节点版本 我最近安装的版本是 4 5 0 LTS 安装后 当我尝试安装新的 npm 时 它不起作用并给出以下错误 C Users myuser npm install g yo npm ERR Windows NT
  • 通过 VBA 传递 Python 参数

    我写了一个python脚本 需要使用VBA调用 我编写了一个 python 脚本 可以解析 pdf 将我需要的数据存储在变量中 并写入预制的 Excel 工作表以用这些值填充单元格 我已经成功地能够调用该脚本并使用以下代码运行它 Sub R
  • Chrome 和 Android 中的 Web SQL 存储限制?

    因此 我正在编写一个 Web 应用程序 需要在离线 Web SQL 数据库中存储约 40MB 的离线数据 它需要在 Chrome 桌面 Safari 桌面和移动 和 Android 浏览器中工作 现在我知道这些浏览器支持 Web SQL 并
  • 如何在 RHEL Linux 服务器上安装 Cargo?

    我尝试在 RHEL 服务器上安装 Cargo curl https sh rustup rs sSf sh 但完成后 我得到回复 cargo bash cargo command not found 有其他安装方式吗 首先启用rhel 7
  • 如何在共享框架中为 XCTest 创建公共扩展?

    例如 我从不使用以下描述XCTestCase expectation 所以我想使用一个函数来为其提供默认值 并通过命名清楚地表明我正在初始化期望 因为您不能真正使用初始化程序XCTestExpectation 但如果扩展不在测试目标中 则无
  • 在 jboss 独立运行时事务无法继续 STATUS_MARKED_ROLLBACK

    有人遇到以下问题吗 我能够使用 jboss 工具在 eclipse 内的 jboss 中成功构建 部署和运行我的 javaee6 应用程序 但是当我们将其部署到另一台独立运行的服务器上时 我们遇到了错误 我在 eclipse 所在的同一台机
  • 片段活动未找到类异常 android

    我正在研究用于姜饼操作系统的片段活动 当我尝试在姜饼模拟器上运行应用程序时 由于以下原因 应用程序被强制关闭ClassNotFound错误 我在下面提供我的主要片段活动代码 和平的帮助将是值得赞赏的 package com example
  • 如何修复docker或重新安装它?

    我已经执行了这个命令 但现在 docker 不工作 如何正确修复 或重新安装 docker sudo rm rf var lib docker aufs 我已经尝试去做 apt get install reinstall docker 但没
  • 在旧版 .NET 平台上使用 .NET Core 包

    我正在使用 VS 中的新 类库 包 模板之一创建 NuGet 包 我想知道 有什么方法可以使用 PCL 中的包吗 not使用 NET Core 例如 我有一个包裹 Enu 具有以下project json title Enu project
  • 调用 Tumblr 帖子转发 api 时收到 401(未授权)

    我在调用 Tumblr 帖子转发 api 时收到 401 状态 其他 tumblr api 工作正常 我关注了 Stackoverflow 的一些链接 但无法使用它 当尝试发布到 tumblr 博客时不断收到 401 https stack
  • ArrayObject, getIterator();

    我试图理解什么getIterator 是的 我将解释 我所知getIterator是我们调用以包含外部迭代器的方法 问题是 getIterator 包含它有自己的方法关闭认为看起来相同的是迭代器接口 但它不能是一个接口 它可以是类 但我试图
  • 使用 Swiftmailer 发送多个附件

    我正在创建一个允许上传多个文件的表单 然后将表单字段值和附件通过电子邮件发送到特定地址 我正在使用 Swiftmailer 生成这些电子邮件 我知道如何使用 Swiftmailer 附加一个文件 但不知道如何附加多个文件 有谁知道使用 Sw
  • UIWebView 未使用 ARC 释放所有活动字节

    我目前正在 iOS 5 1 中构建一个使用 ARC 的导航控制器应用程序 我经常需要显示网页 我制作了一个 Web 查看器 它只是一个 UIWebView 两侧有一些自定义内容 当用户看完页面后 他们点击后退按钮 这将释放与自定义 Web