Note that there are some explanatory texts on larger screens.

plurals
  1. POPreventing UIWebView to allocate memory indefinitely (NSURLCache apparently not working)
    primarykey
    data
    text
    <p>I have been trying to understand how the UIWebView cache works. Since my goal is <strong>to be able to manage the memory allocated by the UIWebView</strong> (at least, as much as possible), to avoid memory raising indefinitely and the App getting killed because of this.</p> <p>After <a href="https://stackoverflow.com/questions/9744644/uiwebview-without-cache">reading</a> <a href="https://stackoverflow.com/questions/2090286/uiwebview-and-nsurlcache-have-a-troubled-relationship">other</a> <a href="https://stackoverflow.com/q/11350978/1264909">stackoverflow</a> <a href="https://stackoverflow.com/questions/1870004/does-nsurlconnection-take-advantage-of-nsurlcache">questions</a> and <a href="http://nshipster.com/nsurlcache/" rel="nofollow noreferrer">searching</a> <a href="http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization" rel="nofollow noreferrer">the</a> <a href="http://petersteinberger.com/blog/2012/nsurlcache-uses-a-disk-cache-as-of-ios5/" rel="nofollow noreferrer">web</a>, I decided to try the <strong>NSURLCache sharedURLCache</strong>, but I cannot figure out how it really works.</p> <p><strong>My testing scenario is this one:</strong></p> <p>I have implemented a test app for <strong>iOS 5</strong> where I have a single view with a UIWebView inside. This UiWebview is going to load a local <em>index.html</em> file like this:</p> <pre><code>// Create an NSString from the local HTML file NSString *fullURL = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]; NSURL *url = [NSURL fileURLWithPath:fullURL]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; // Load local HTML file [self.webView loadRequest:requestObj]; </code></pre> <p>In this <em>index.html</em> file, I have a JS script that downloads a JSON from Flickr with the last images uploaded to the their public feed. Each image is appended to a list. This whole process is repeated every second, until we reach 1000 images downloaded from the feed. See the code:</p> <pre><code>var nImg = 0; function getData() { $.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?format=json&amp;jsoncallback=?', function(data) { var items = []; $.each(data.items, function(i, image) { items.push('&lt;li id="' + image.title + '"&gt;&lt;img src="' + image.media.m + '"/&gt;&lt;/li&gt;'); }); $('#page &gt; #imagesList').append(items.join'')); nImg += items.length; }); $('#page &gt; #numImages').html(nImg); if(nImg &lt; 1000) setTimeout("getData();", 1000); // Get more data in 1s } $(document).ready(function() { getData(); }); </code></pre> <p>Finally, in my AppDelegate, I set up the sharedURLCache as written in this <a href="http://nshipster.com/nsurlcache/" rel="nofollow noreferrer">post</a>:</p> <pre><code>// Initialize the Cache, so we can control the amount of memory it utilizes int cacheSizeMemory = 4*1024*1024; // 4MB int cacheSizeDisk = 32*1024*1024; // 32MB NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:nil]; [NSURLCache setSharedURLCache:sharedCache]; </code></pre> <p>But, despite what it is supposed to happen, when I open instruments and run the app checking the allocations, the memory keeps growing while we download images (up to 20MB), instead of flattening around 4-6MB as I would expect if the UIWebView would be using this sharedURLCache to cache the images downloaded.</p> <p><img src="https://i.stack.imgur.com/yn682.png" alt="Instruments evolution after we reach 1000 images"></p> <p>Does anybody know what am I doing wrong? Do I have misunderstood something? Am I loading the page wrong? Does the UIWebView uses another cache for the images loaded in the JS?</p> <p>Please, let me know your thoughts. I really need to understand how this sharedURLCache works, and how the UIWebView uses it to cache URLRequest, images,... in case it is used at all. <strong>I do not want to have an App that uses a UIWebView that can allocate memory without control.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload