Fergal Rooney
Fergal Rooney

Reputation: 1390

UIWebView Memory Management tips

I am developing an iOS application which makes heavy use of multiple UIWebViews. All the request's are loaded from local html files within the application bundle. I found awesome tips for attempting the prevent memory leaks on deallocation here: http://www.codercowboy.com/code-uiwebview-memory-leak-prevention

What I am looking to do is create a Factory class which holds a pool of 3 UIWebView instances, and any time a UIWebView is requested, it should request an instance from this pool. So I will never be deallocating the WebViews unless I run into a memory warning where I may deallocate the WebViews if neccessary.

This is what I have thought of so far, and am looking for any extra tips or comments on my suggestions.

  1. Once the pool is full, a WebView will have to be reused. It will be a first in first out type stack.
  2. Once a webview is requested to be reused, it will execute a destroy method defined in the HTML implementation to null out global variables. This can be done using the UIWebView's stringByEvaluatingJavascriptFromString
  3. Once that has completed, the WebView will execute document.innerHTML = ""; to clear out the document.
  4. The re-used UIWebView will then load the new request.
  5. I hear use of multiple images can use a large amount of memory. Would it be of benefit to replace all tags sources with a tiny image when a UIWebView in the pool is not on screen?

Appreciate any input here!

Thanks, Fergal.

Upvotes: 0

Views: 2958

Answers (2)

Zahid
Zahid

Reputation: 562

Try using paginated web page and you can cache data for pages using NSURLProtocol.

It would load pages and cache onto disk so navigation backward would be from cache and only 1 page would be in memory which is being viewed.

That is best you can do with UIWebView OR you may go for MKWebView it can allow you additional features.

Upvotes: 0

Gio MV
Gio MV

Reputation: 105

Instead of reloading the content using loadHTMLFromString or loadRequest, continue to use stringByEvaluatingJavascriptFromString to set the innerHTML of a section with an specific ID with the content you want, all through javascript, which won't use a big amount of memory

Upvotes: 1

Related Questions