7
votes

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

2

2 Answers

6
votes

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

0
votes

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.