Memory leak with UIWebView
Asked Answered
A

2

7

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

Aerotherapeutics answered 9/2, 2015 at 1:47 Comment(0)
D
7

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

Dodecanese answered 13/2, 2015 at 19:38 Comment(7)
WKWebView does not inherit from UIWebView, it's a completely new and different class. If you're on iOS 8 only you should definitely use it over UIWebView. You should also be using ARC.Fatsoluble
@jshier - Thanks for the catch... I edited the answer from what I meant to say (Which is they share many of the same method call names making it a very easy switch).Dodecanese
thank you so much for your solution, WKWebView works for my case!Aerotherapeutics
so whats the solution if we need to support iOS 7?? I've tried the cache and other solutions with no luck. i agree wkwebview works great on iOS 8Choral
I have not spent the time their since I was building an ios8 only app. Sorry.Dodecanese
shoot, thanks. just can't believe this is such a huge issue with no solution anywhereChoral
Every time I was opening my UIWebView, it ate up a permanent ~30MB chunk of memory as seen in Instruments Allocations. The Leaks application never caught anything under the UIWebView, so it wasn't technically a memory leak at least according to the Leaks app, but the heap would continuously be used up in the persistent memory footprint of the app. Once I switched over to WKWebView, there was no problem. I should add that my app was previewing PNG images via the web view and the memory use was sourced to ImageIO, similar to one the other answer.Dorice
C
0

I had a similar problem, having users testing the app by previewing images in UIWebView. The app would crash after N previews. Using Apple Instruments tool with Allocations profiling template. From the tool, I was able to select the following Allocation Lifespan: "Created & Persistent". Further observations is when previewing the same file multiple times, Persistent Bytes (Based on apple definition, this is the number of bytes that have been allocated, but not released.) for ImageIO_jpeg_Data keep growing in doubles, this is true for any other image type.

A resolution for this is to use UIImageView from apple as a separate previewer for Images, this cause no memory leaks at all when previewing images.

Calculator answered 4/6, 2015 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.