Memory leak using UIWebView

My project is a hybrid static library for displaying a UIWebView with some JS for managing logic. When I use the 64-bit version and run the demo on iOS 8 / iPhone 6, the memory continues to work up to 30 M or more!

  • When I use generation in a tool, increased memory usage is almost all of webcore; Does this mean that there are leaks in the JS code? I cannot find a leak when I use Safari to run similar JS directly.

  • When I free UIWebView, the memory is still not freed; I tested using a distribution tool. There are still some web pages and (not objects) in my 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 am using 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 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.

What is the correct way to release UIWebView?

IOS 8 memory management UIWebView

Leaks UIWebView, JS Scavenger and WebCore VMs

Free memory / cookie / cache from UIWebView after closing

+6
source share
2 answers

I had the same problem and switched to the new WKWebView and it immediately resolved all the memory leak problems that I saw. WKWebView shares many of the same call names from UIWebView , so all I needed to do in my project was to switch all the objects of my UIWebView to "WKWebView" and the memory leak disappeared.

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

Apple Documentation

+6
source

I had a similar problem when users tested the application while viewing images in a UIWebView. The application will crash after N views. Using Apple Instruments with the Allocations profiling template. From the tool, I was able to choose the following distribution duration period: "Created and Permanent." Further observations are when you look at the same file several times, Persistent Bytes (based on Apple's definition, this is the number of bytes that were allocated but not released.) For ImageIO_jpeg_Data, it continues to grow in two-local ones, this is true for any other type of image .

The solution for this is to use UIImageView from apple as a separate preview for images, this does not cause memory leaks when previewing images.

0
source

Source: https://habr.com/ru/post/982181/


All Articles