Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView memory management

I have a problem with memory management.

I am developing an application that makes heavy use of UIWebView. This app generates dynamically lots of UIWebViews while loading content from my server. Some of these UIWebViews are quite large and have a lot of pictures.

If I use instruments to detect leaks, I do not detect any. However, lots of objects are allocated and I suspect that has to do with the UIWebViews.

When the webviews release because no longer needed, it appears that not all memory is released. I mean, after a request to my server the app creates an UITableView and many webviews (instruments say about 8Mb). When user tap back, all of them are released but memory usage only decrements about 2-3 Mb, and after 5-10 minutes using the app it crashes.

Am I missing something? Anyone know what could be happening?

Thank you!

like image 723
wolfrevo Avatar asked Jun 01 '10 15:06

wolfrevo


People also ask

Is UIWebView deprecated?

As of iOS 12.0, 'UIWebView' has been explicitly marked as deprecated. Apple is encouraging developers to adopt WKWebView instead, which was introduced in iOS 8.0.

What does UIWebView mean?

Android is powered by Chrome. Mobile Safari UIWebView. The UIWebView is different from the ordinary Safari browser, as it is not a stand-alone browser, but merely browser functionality that is embedded in a third party app that allows the app to display content from the web.

What is the difference between UIWebView and WKWebView?

Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.

What is a UIWebView iPhone?

A view that embeds web content in your app.


2 Answers

This isn't a solution, but a possible explanation of the issues you're facing.

I had memory problems with an app I worked on. It consisted of (in effect) a scroll view, containing 3 full screen sized web views. Each webview's content size was multiple screens, say 10 on average.

When you load a webview, the whole view is rendered in memory. SO, aside from the memory used to load the images and other data (the stuff you see in the Allocations instrument), the web view itself uses memory when it's rendered that you have no control over.

So, for example, a 768x1024 webview, with 10 pages of content would use

768 * 1024 * 10 (pages) * 4 (bits per pixel) = 31,457,280 = 30MB.

Multiply by 3 web views, and that's 90MB used on top of memory that the app allocated directly.

You can get a feel for how much memory is being used like this in Instruments, using the VM Tracker instrument.

like image 128
Ashley Mills Avatar answered Sep 23 '22 15:09

Ashley Mills


Try adding

[webView loadHTMLString: @"" baseURL: nil];

right before you release the webview. For a leak in 4.2.1 relating to displaying a PDF in a UIWebView this solves most of the leak problems for me.

like image 39
Dad Avatar answered Sep 22 '22 15:09

Dad