Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak with UIWebView

My project is a hybrid static lib for showing a UIWebView with some JS to control the logic. When I use 64bit and run demo on iOS 8/iPhone 6, the memory keeps going to 30M or more!

  1. When I use generation in instrument, the increased memory usage is almost all from webcore; does it means there are leaks in JS code? I can't find a leak when I use Safari to run similar JS directly.

  2. When I release the UIWebView, the memory is still not freed; I tested with instrument allocation. There are some webcore and (non - object) still in memory, what can I do to release them?

    • 0JavaScriptCore WTF::MallocHook::recordAllocation(void*, unsigned long)
    • 1 JavaScriptCore WTF::fastMalloc(unsigned long)
    • 2 WebCore WebCore::SharedBuffer::buffer() const
    • 3 WebCore WebCore::SharedBuffer::data() const
    • 4 WebCore WebCore::ResourceLoader::didReceiveDataOrBuffer(char const*, unsigned int, WTF::PassRefPtr, long long, WebCore::DataPayloadType)
    • 5 WebCore WebCore::SubresourceLoader::didReceiveDataOrBuffer(char const*, int, WTF::PassRefPtr, long long, WebCore::DataPayloadType)
    • 6 WebCore WebCore::SubresourceLoader::didReceiveBuffer(WTF::PassRefPtr, long long, WebCore::DataPayloadType)
    • 7 WebCore WebCore::ResourceLoader::didReceiveBuffer(WebCore::ResourceHandle*, WTF::PassRefPtr, int)
    • 8 WebCore WebCore::SynchronousResourceHandleCFURLConnectionDelegate::didReceiveDataArray(__CFArray const*)

I use the following code.

-(void)createUIWebview{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:serviceUrl]]];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}

-(void)dealloc{
if (_webView.isLoading){
    [_webView stopLoading];
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
_webView.delegate=nil;
[_webView removeFromSuperview];
[_webView release];
_webView = nil;
}

I have researched the following links, but they don't seem to solve my problem. Is UIWebview still leaking in iOS 8? And the problem seems not so obvious when I use iOS 6 in iPhone4.

Whats the proper way to release a UIWebView?

iOS 8 UIWebView memory management

UIWebView leaks, JS Garbage Collector & WebCore VMs

Release memory/cookie/cache from UIWebView once closed

like image 342
shan li Avatar asked Feb 09 '15 01:02

shan li


1 Answers

I was having the same problem and switched to the new WKWebView and it immediately solved all of the memory leak issues I was seeing. WKWebView shares many of the same call names from UIWebView so all I had to do on my project is switch over all my `UIWebView' objects to 'WKWebView' and the memory leaks went away.

Remember to import the WebKit into your project and know that is is only available on iOS8.

Apple Documentation

like image 90
Chase S Avatar answered Oct 19 '22 23:10

Chase S