Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8344551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:24:30+00:00 2026-06-09T06:24:30+00:00

I am trying to fix a bunch of leaks that my UIWebView is causing

  • 0

I am trying to fix a bunch of leaks that my UIWebView is causing and cannot find their origin nor a workaround. What I do is getting some content from the web through a network request, then assemble my HTML and load it on the fly:

NSString* body = <some HTML>;
NSString* html = [NSString stringWithFormat:kHTMLTemplate, [self scripts], [self styles], body];
[_webView loadHTMLString:html
               baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

Each time there is new content available, I execute loadHTMLString again to refresh the web view. I reuse the same web view, same controller, same everything.

Instruments shows a very strange pattern in which all leaked objects are General-blocks of various sizes and none of them has any information attached to it: no responsible library, no responsible frame, etc. Each time loadHTMLString is executed, new leaks are added.

It seems that there are several threads in S.O. about UIWebView leaking memory. I have tried all suggestions I found (e.g., setting the NSURLCache to zero, or resetting it; I tried releasing the existing UIWebView and allocate a fresh one each time I have new data, etc.) but nothing has helped.

My investigations up to now lead to one clear outcome: it seems that the leaks are only present if the HTML I load into the view contains some Javascript. If you notice the html string above, it is made up of several components; one is [self scripts] which is a function that simply returns:

return @"<script type='text/javascript' src='jquery-1.4.4.min.js'></script>"
        "<script type='text/javascript' src='jmy.js'></script>";

If I remove this, no leaks are there. But the leaks appear as soon as I add a <script> tag to my HTML. They even appear if I simply include the jquery file (or any other js file, as to this):

return @"<script type='text/javascript' src='jquery-1.4.4.min.js'></script>";

So, the question: has anyone an idea about what is happening here? Clearly including a Javascript file into my HTML is making the UIWebView leak memory.

The fact that leaks appear both when I reuse the same UIWebView object or when I instantiate a new one each time I have content, leads me to think that there must be something in the way javascript files are handled by loadHTMLString which leads to the leaks.

Does anyone know how this could be fixed?

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:24:32+00:00Added an answer on June 9, 2026 at 6:24 am

    I finally found some clue at what is happening and above all a workaround that I would like to share.

    I can confirm that the simple inclusion of some javascript file was causing a memory leak on reload of the web view. I even tried building a file with the HTML content, then loading it into the UIWebView through loadRequest, and reloading it through reload; the leaks were always there. I will post a radar for that.

    What saved me was using innerHTML to update the content of the web view. Instead of relying on reload or loadHTMLString, I initialized my web view with an empty body (I mean, the head section was there, including all required JS/CSS files) then updated it setting document.body.innerHTML:

    body = [body stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setBody(\"%@\");", body]];
    

    with setBody defined like this:

    var setBody = function(body) {
        document.body.innerHTML = body;
    }
    

    I obtained two benefits: the web view update became really fast (this is an effect of not updating the DOM, which on the other hand is not entirely desirable on the whole), and there were no memory leaks were running the app under Instruments. The drawback was that I had to fine-tune a couple of conditions where the app was running fine; specifically:

    1. loading the web view (even with an empty body page) take a lot, so you have to synchronize the first update of its content to when the DOM is ready;

    2. webViewDidFinishLoading seems not reliable: it is executed before document.readyState becomes complete;

    3. document.documentElement.height, the official way of retrieving the page height seems not reliable, too: workaround is getting the “computed style” of the body part and read its height value.

    Hope this helps someone else who finds that his web views are leaking memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to fix a bunch of email addresses that I have in
I've got some bad MySQL entries that I need to fix. I'm trying to
I've been trying to fix this problems for a few hours already, I cannot
I am trying to fix some HTML, and it is working perfect. But one
Could use some help. I am utterly stuck, been trying to fix this one
I have a bunch of collection-like COM interfaces that I'm trying to write an
I'm trying to show a bunch of images in a panel. That panel would
Basically, I'm trying to create an application that features a bunch of colored rectangles
I'm trying to "fix" a build on our TF Server that drops everything into
I am trying to create some methods that should make it easier to perform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.