UIWebView不会释放内存 [英] UIWebView is not deallocating memory
问题描述
我在尝试恢复我在应用程序中为 UIWebView
分配的内存时遇到了一些麻烦。我基本上为用户在单独的 ViewController
中创建并呈现 UIWebView
,然后删除所有引用并弹出<来自堆栈的code> ViewController 。尽管做了所有这些,但是分配的内存永远不会被返回,而且我每次都会遇到额外的10 Mb内存 我认为 ViewController
再次。
I'm having some real trouble trying to recover the memory I've allocated for a UIWebView
within my application. I essentially create and present the UIWebView
temporarily for the user in a separate ViewController
, then remove all references and pop the ViewController
from the stack. Despite doing all of this, the memory allocated is never returned, and I get stuck with an extra 10 Mb of memory each time I segue to that ViewController
again.
让我们看看我是如何管理这个 UIWebView
无论如何。
Let's see how I'm managing this UIWebView
anyways.
嗯,首先,我在我的头文件中有一个本地引用,显示在这里
Well, for starters, I've got a local reference to it in my header file, show here
#pragma mark UIWebView Properties
/// WebView for loading URL resources
@property (nonatomic, weak)UIWebView *webView;
请注意,它不是 IBOutlet
,I在以下方法中以编程方式创建此 UIWebView
,在 ViewDidAppear
中触发。
Notice that it's not an IBOutlet
, I'm creating this UIWebView
programmatically in the following method, fired in ViewDidAppear
.
-(void)presentYoutubeVideoWithID:(NSString *)videoID {
/* Setup UIWebView */
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
/* Set Delegate */
[webView setDelegate:self];
/* Set local property */
[self setWebView:webView];
/* Add view to ViewController */
[self.view addSubview:webView];
/* Set constraints */
// Removed for simplicity
/* Set URL */
NSString *youTubeVideoHTML = @"<!DOCTYPE html> (Removed) </html>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, self.view.frame.size.width, self.view.frame.size.height, videoID];
[webView loadHTMLString:html baseURL:[[NSBundle mainBundle]resourceURL]];
/* Setup Notification */
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleWhenDoneButtonClick:)
name:UIWindowDidBecomeHiddenNotification
object:nil];
}
最后,我清理并弹出 UIViewController
:
-(void)handleWhenDoneButtonClick:(NSNotification *)notification {
/* Remove Observer */
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:self.view.window];
/* Remove UIWebView from hierarchy */
[self.webView removeFromSuperview];
/* Remove self from webView delegate */
[self.webView setDelegate:nil];
/* Remove reference because I can */
[self setWebView:nil];
/* Dismiss ViewController */
[self dismissViewControllerAnimated:YES completion:nil];
}
对于一个简单的场景,似乎没有任何东西真正消除了占用的内存UIWebView。正如你在这里看到的,内存使用量永远不会减少:
For a simple enough scenario, nothing seems to actually remove the memory taken up by the UIWebView. As you can see here, the memory usage never decreases:
在加载UIWebView之前:
UIWebView打开后:
弹出ViewController并删除对UIWebView的引用后:
据我所知,此处使用的内存量无限期地持续存在。五分钟后,它仍将分配34+ Mb。如果我再次打开 ViewController
,它将分配更多,一旦我再次弹出,我将最终获得大约40 Mb的基本使用量。
The amount of memory used here persists indefinitely as far as I can tell. After five minutes, it will still have 34+ Mb allocated. If I open the ViewController
again, it will allocate more, and I will end up with about 40 Mb of "base" usage once I pop again.
泄漏:
没有被完全劝阻,我最初去寻找泄漏,以为我在某处犯了一些错误。然而,虽然我确实发现了一些非常小泄漏,但它们似乎与 UIWebView
完全相关。
Leaks:
Not being completely dissuaded, I initially went off to look for leaks, thinking I had made some mistake somewhere. However, while I did actually find some very small leaks, they were seemingly entirely related to UIWebView
.
SO上的其他案例:
我'我已经花了很长时间在网上寻找解决方案,但我发现的唯一真正相关的线程是去年的一个,如下所示: UIWebView占用大量内存
它还没有答案,这可能是因为(像大多数与此主题相关的其他问题),很多回复都提供了通用/冲突的建议。比如建议你使用ARC(谁没有?),或告知OP他们不应该保留强引用(即使它们不是,代码也证明了这一点)。最后,一些答案只是声称内存管理不是一个问题,因为它足以绕过,而且那些也没有帮助。
It doesn't have an answer yet, and that is probably because (Like most other questions related to this topic), a lot of the replies provide generic/conflicting advice. like suggesting you use ARC (Who doesn't?), or informing OP that they shouldn't keep strong references (Even when they aren't, and the code demonstrates that). Finally, some answers just claim that memory management isn't a problem to begin with because there's enough to go around, and those aren't helpful replies either.
无论如何,这是我面临的当前问题。感谢您花时间阅读本文(如果您这样做了!)。
Anyways, this is the current issue I am facing. Thank you for taking the time to read this (If you did!).
推荐答案
我遇到了同样的问题。在iOS 9设备中使用 UIWebView
(加载google.com)推送 UIViewController
时,内存大小来自弹出控制器后,26 MB到36 MB并保持不变。
I have faced the same issue. When pushing a UIViewController
with a UIWebView
(loading google.com) in an iOS 9 device, the memory size went from 26 MB to 36 MB and it stayed the same after popping the controller.
我使用 WKWebView进行了相同的测试
(iOS 8+)而不是 UIWebView
。这次,内存大小从26 MB增加到~27.5 MB,并在弹出控制器后回落到~26 MB。
I did the same test using WKWebView
(iOS 8+) instead of UIWebView
. This time, the memory size went from 26 MB to ~27.5 MB and it dropped back to ~26 MB after popping the controller.
似乎问题部分到期了实现 UIWebView
的方式。也许你可以使用 https://github.com/floatlearning/FLWebView ,这是 WKWebView
,带 UIWebView
iOS回退。
It seems that the problem is partly due to the way UIWebView
is implemented. Maybe you could use https://github.com/floatlearning/FLWebView, which is a WKWebView
with UIWebView
fallback for iOS.
这篇关于UIWebView不会释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!