无法从UIWebView完全回收内存使用情况 [英] Unable to Completely Reclaim Memory Usage from UIWebView

查看:66
本文介绍了无法从UIWebView完全回收内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例代码(使用ARC),它将 UIWebView 添加为子视图,然后将其删除(通过屏幕上的点按手势切换):

I have the following sample code (using ARC) which adds a UIWebView as a subview and subsequently removes it (toggled by a tap gesture on the screen):

- (void)toggleWebViewLoading:(UITapGestureRecognizer *)sender
{
    if (webView == nil)
    {
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0f, 100.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 100.0f)];
        [webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"http://www.google.ca"]]];
        [self.view addSubview:webView];
    }
    else
    {
        [webView removeFromSuperview];
        webView = nil;
    }
}

当应用最初加载空白<$ c时$ c> UIView ,它消耗大约 1.29 MB (从Instruments.app读取的实时字节数)。点击空白 UIView 会导致 toggleWebViewLoading:函数触发,从而创建新的 UIWebView ,加载Google,并将其添加为子视图。完成此操作序列后,内存使用量约为 3.61 MB 。点击再次执行的第二部分toggleWebViewLoading:,从其superview中删除 UIWebView 并将其设置为。此时内存消耗已降至 3.38 MB

When the app initially loads with a blank UIView, it consumes approximately 1.29 MB (live bytes reading from Instruments.app). Tapping on the blank UIView causes the toggleWebViewLoading: function to fire which in turn creates a new UIWebView, loads Google, and adds it as a subview. Once this sequence of operations have been completed, the memory usage is approximately 3.61 MB. Tapping again executes the second part of toggleWebViewLoading: which removes the UIWebView from its superview and sets it to nil. At this point the memory consumption has dropped to 3.38 MB.

我的问题是,如何从UIWebView中完全回收内存(即内存消耗恢复到 1.29 MB的初始值 UIWebView 被删除并设置为 nil 后,code>或类似内容?

My question is, how can I completely reclaim the memory from UIWebView (i.e. have the memory consumption return to its initial value of 1.29 MB or something similar after the the UIWebView has been removed and set to nil)?

在有人问我为何如此关心 ~2之前MB 节省内存,我有更复杂的情况使用Xamarin / MonoTouch 10+ UIWebView 正在消耗 200 MB + 的内存,我似乎无法在不再需要时回收所有内存。我认为答案归结为这个简单的问题。

Before someone asks why I care so much about ~2 MB of memory savings, I have I much more complicated situation using Xamarin/MonoTouch where 10+ UIWebView's are consuming 200 MB+ of memory and I can't ever seem to reclaim all of the memory when it is no longer needed. I think the answer boils down to this simple question.

推荐答案

我建议您监控活动的线程数。 UIWebView为自己生成了几个线程。一旦取消分配UIWebView,我希望它们不会被正确清理,但这只是一种预感。

I would suggest monitoring how many threads you have active. UIWebView spawns several threads for itself. I expect they are not cleaned up properly once the UIWebView is deallocated, but it's just a hunch.

这篇关于无法从UIWebView完全回收内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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