Code – UIWebView Memory Leak Prevention

Preventing memory leaks related to UIWebView can be difficult. Apple’s documentation on the subject is sparse, and not straight forward, simply because the UIWebView is a complex beast with many underlying classes used within.

This Objective-C Category represents a collection of ideas, and theories (think magic rather than science..) collected from various web blogs and discussions on the subject.

For more Objective-C memory management ideas, check out WIJL: IOS Memory Management.

Using the Category is simple:

  1. Download the code: UIWebView+Clean.zip
  2. Add the UIWebView+Clean.h and .m files to your project.
  3. Import “UIWebView+Clean.h” in any files you want to use the category in.
  4. Use the cleanForDealloc method anywhere you intend to deallocate a UIWebView:
    1. [self.webView cleanForDealloc];
    2. self.webView = nil;

Here’s the code:

UIWebView+Clean.h

  1. /*
  2.  
  3.     UIWebView+Clean.h
  4.    
  5.     A simple category that performs recommended UIWebView clean before dealloc
  6.    
  7.     Created by Jason Baker (jason@onejasonforsale.com) for TumbleOn on 3/18/12.
  8.  
  9.     Copyright (c) 2012, Pocket Sized Giraffe, LLC
  10.     All rights reserved.
  11.  
  12.     Redistribution and use in source and binary forms, with or without
  13.     modification, are permitted provided that the following conditions are met:
  14.  
  15.     1. Redistributions of source code must retain the above copyright notice, this
  16.     list of conditions and the following disclaimer.
  17.     2. Redistributions in binary form must reproduce the above copyright notice,
  18.     this list of conditions and the following disclaimer in the documentation
  19.     and/or other materials provided with the distribution.
  20.  
  21.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  22.     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23.     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24.     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  25.     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26.     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27.      LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28.     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29.     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  
  32.     The views and conclusions contained in the software and documentation are those
  33.     of the authors and should not be interpreted as representing official policies,
  34.     either expressed or implied.
  35.  
  36.  */
  37.  
  38. #import <Foundation/Foundation.h>
  39.  
  40. @interface UIWebView (clean)
  41.  
  42. // performs various cleanup activities recommended for UIWebView before dealloc.
  43. // see comments in implementation for usage examples
  44. - (void) cleanForDealloc;
  45.  
  46. @end

UIWebView+Clean.m

  1. /*
  2.  
  3.  UIWebView+Clean.h
  4.  
  5.  A simple category that performs recommended UIWebView clean before dealloc
  6.  
  7.  Created by Jason Baker (jason@onejasonforsale.com) for TumbleOn on 3/18/12.
  8.  
  9.  Copyright (c) 2012, Pocket Sized Giraffe, LLC
  10.  All rights reserved.
  11.  
  12.  Redistribution and use in source and binary forms, with or without
  13.  modification, are permitted provided that the following conditions are met:
  14.  
  15.  1. Redistributions of source code must retain the above copyright notice, this
  16.  list of conditions and the following disclaimer.
  17.  2. Redistributions in binary form must reproduce the above copyright notice,
  18.  this list of conditions and the following disclaimer in the documentation
  19.  and/or other materials provided with the distribution.
  20.  
  21.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  22.  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23.  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24.  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  25.  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26.  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27.  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28.  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29.  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  
  32.  The views and conclusions contained in the software and documentation are those
  33.  of the authors and should not be interpreted as representing official policies,
  34.  either expressed or implied.
  35.  
  36.  */
  37.  
  38. #import "UIWebView+Clean.h"
  39.  
  40. @implementation UIWebView (clean)
  41.  
  42. /*
  43.  
  44.  Using this Category is easy, simply add this to the top of your file where
  45.  you have a UIWebView:
  46.  
  47.  #import "UIWebView+Clean.h"
  48.  
  49.  Then, any time you want to throw away or deallocate a UIWebView instance, do
  50.  the following before you throw it away:
  51.  
  52.  [self.webView cleanForDealloc];
  53.  self.webView = nil;
  54.  
  55.  If you still have leak issues, read the notes at the bottom of this class,
  56.  they  may help you.
  57.  
  58.  */
  59.  
  60.  
  61. - (void) cleanForDealloc
  62. {
  63.     /*
  64.      
  65.      There are several theories and rumors about UIWebView memory leaks, and how
  66.      to properly handle cleaning a UIWebView instance up before deallocation. This
  67.      method implements several of those recommendations.
  68.      
  69.      #1: Various developers believe UIWebView may not properly throw away child
  70.      objects & views without forcing the UIWebView to load empty content before
  71.      dealloc.
  72.      
  73.      Source: http://stackoverflow.com/questions/648396/does-uiwebview-leak-memory
  74.      
  75.      */        
  76.     [self loadHTMLString:@"" baseURL:nil];
  77.    
  78.     /*
  79.      
  80.      #2: Others claim that UIWebView's will leak if they are loading content
  81.      during dealloc.
  82.      
  83.      Source: http://stackoverflow.com/questions/6124020/uiwebview-leaking
  84.      
  85.      */
  86.     [self stopLoading];
  87.    
  88.     /*
  89.      
  90.      #3: Apple recommends setting the delegate to nil before deallocation:
  91.      "Important: Before releasing an instance of UIWebView for which you have set
  92.      a delegate, you must first set the UIWebView delegate property to nil before
  93.      disposing of the UIWebView instance. This can be done, for example, in the
  94.      dealloc method where you dispose of the UIWebView."
  95.      
  96.      Source: UIWebViewDelegate class reference    
  97.      
  98.      */
  99.     self.delegate = nil;
  100.    
  101.    
  102.     /*
  103.      
  104.      #4: If you're creating multiple child views for any given view, and you're
  105.      trying to deallocate an old child, that child is pointed to by the parent
  106.      view, and won't actually deallocate until that parent view dissapears. This
  107.      call below ensures that you are not creating many child views that will hang
  108.      around until the parent view is deallocated.
  109.      */
  110.    
  111.     [self removeFromSuperview];
  112.    
  113.     /*
  114.      
  115.      Further Help with UIWebView leak problems:
  116.      
  117.      #1: Consider implementing the following in your UIWebViewDelegate:
  118.      
  119.      - (void) webViewDidFinishLoad:(UIWebView *)webView
  120.      {
  121.         //source: http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part1-memory-leaks-on-xmlhttprequest/
  122.         [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
  123.      }
  124.      
  125.      #2: If you can, avoid returning NO in your UIWebViewDelegate here:
  126.      
  127.      - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
  128.      {
  129.         //this source says don't do this: http://stackoverflow.com/questions/6421813/lots-of-uiwebview-memory-leaks
  130.         //return NO;
  131.         return YES;
  132.      }
  133.      
  134.      #3: Some leaks appear to be fixed in IOS 4.1
  135.      Source: http://stackoverflow.com/questions/3857519/memory-leak-while-using-uiwebview-load-request-in-ios4-0
  136.      
  137.      #4: When you create your UIWebImageView, disable link detection if possible:        
  138.      
  139.      webView.dataDetectorTypes = UIDataDetectorTypeNone;
  140.      
  141.      (This is also the "Detect Links" checkbox on a UIWebView in Interfacte Builder.)
  142.      
  143.      Sources:
  144.      http://www.iphonedevsdk.com/forum/iphone-sdk-development/46260-how-free-memory-after-uiwebview.html
  145.      http://www.iphonedevsdk.com/forum/iphone-sdk-development/29795-uiwebview-how-do-i-stop-detecting-links.html
  146.      http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/
  147.      
  148.      #5: Consider cleaning the NSURLCache every so often:
  149.      
  150.      [[NSURLCache sharedURLCache] removeAllCachedResponses];
  151.      [[NSURLCache sharedURLCache] setDiskCapacity:0];
  152.      [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  153.      
  154.      Source: http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/
  155.      
  156.      Be careful with this, as it may kill cache objects for currently executing URL
  157.      requests for your application, if you can't cleanly clear the whole cache in
  158.      your app in some place where you expect zero URLRequest to be executing, use
  159.      the following instead after you are done with each request (note that you won't
  160.      be able to do this w/ UIWebView's internal request objects..):
  161.      
  162.      [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
  163.      
  164.      Source: http://stackoverflow.com/questions/6542114/clearing-a-webviews-cache-for-local-files  
  165.      
  166.      */
  167. }
  168.  
  169. @end