UIWebview eats around 40mb memory

Discussion in 'iOS Programming' started by Samppaa, Mar 18, 2011.

  1. Samppaa macrumors regular

    Mar 26, 2010
    So, I finally joined the developer program and tested my app on the device. It turns out that app is using around 40-50mb memory which is bad. I have measured that the UIWebView uses around 30-40mb of memory when I send a request to it. So I have tried many solutions like releasing the webview and such, but no luck. My apps memory usage is around 15mb, but when the webview loads a page it jumps to ~50mb as I told. I have searched around the internet, but can't seem to find a working solution. Has anyone else expirienced this kind of behavior before? Really sad this kind of situation comes right before I am ready to move the app to the store. Thanks in advance, I would be forever thankful!

    Note: The webview is hidden, as I need to perform javascript on it and after AJAX has loaded, get the source code. It has no other use.
  2. Manty macrumors member

    Mar 18, 2008
    Lisbon, Portugal
    There is a bug in the SDK when the UIWebView background color is the scroll view background texture. What background color are you using?
  3. hayesk macrumors 65816

    May 20, 2003
    Two things:

    1. I'm actually not surprised that the web view takes up 40MB. The rendering and javascript engine takes up RAM. I think you'll also find, that it's only the first webview that takes so much. If you make two, I'll bet it won't take twice the RAM. I think the initial hit of allocating the rendering and JS engines is the culprit.

    2. When you release the web view, do you get the memory back? Run it through with the Leaks tool in Instruments. See if there are leaks.

    If you don't have leaks in your own code, then there is nothing more for you to do but accept the fact that UIWebView takes a lot of RAM.

    However, are you scraping data from someone else's site or your own? If it's your own, investigate whether you can put something on the server to give you the code you want in the first place, without having to run it through a web browser first. That's ripe with problems - what happens when the web page changes?
  4. Samppaa, Mar 20, 2011
    Last edited: Mar 20, 2011

    Samppaa thread starter macrumors regular

    Mar 26, 2010
    Manty - I use the default UIWebView I haven't changed any texture.

    Hayesk - When I try to release the webView I don't seem to get the memory back. And I am taking data from someone elses website, the site is not my own. Only leaks I see in the leaks tool are something like "Generalblock 2048 and GeneralBlock-56" I really have no idea about them, but I read that they are false positives or something...

    Thanks for your comments!
  5. hayesk macrumors 65816

    May 20, 2003
    If you don't get the memory back, it sounds like you are still retaining the view. Posting your viewcontroller code that owns the web view may help. But if it doesn't show up as a leak, then you likely are still retaining it and don't realize it. Remember [[class alloc] init] and copy will retain. So if you have a property like:
    @property (nonatomic,retain) UIWebView *myWebView;
    and then in your view controller you have:
    self.myWebView = [[UIWebView alloc] init];
    you have actually retained it twice - one for the init, and one for setting the property. It should be something like:
    self.myWebView = [[[UIWebView alloc] init] autorelease];
    UIWebView *aWebView = [[UIWebView alloc] init];
    self.myWebView = aWebView;
    [aWebView release];
  6. Samppaa thread starter macrumors regular

    Mar 26, 2010
    I have the webview connected as IBOutlet I have placed it in interface editor.
  7. grinchdee macrumors newbie

    Mar 30, 2011
    I have a similar issue on the leak. What is your device iOS version? In my case, I only have leak with 4.3/4.3.1 but no leak with 4.2.1 and 3.2.2.
  8. North Bronson macrumors 6502

    Oct 31, 2007
    San José
    You might also try something like:
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    if the application is holding on to any cached data from the web view.

Share This Page