Prevent UIWebView for temporary memory allocation (NSURLCache does not seem to work)

I am trying to understand how the UIWebView cache works. Since my goal is to be able to manage the memory allocated by the UIWebView (at least as much as possible), in order to avoid expanding the memory size indefinitely, and because of this, the application will be killed because of this.

After reading another stackoverflow questions and searching the web , I decided to try NSURLCache sharedURLCache , but I can't figure out how this works.

My testing scenario:

I have implemented a test application for iOS 5 , where I have one view with a UIWebView inside. This UiWebview is about to load the local index.html file as follows:

// 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]; 

In this index.html file, I have a JS script that loads JSON from Flickr with the latest images uploaded to their shared channel. Each image is added to the list. This whole process is repeated every second until we reach 1000 images downloaded from the feed. See code:

 var nImg = 0; function getData() { $.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?format=json&jsoncallback=?', function(data) { var items = []; $.each(data.items, function(i, image) { items.push('<li id="' + image.title + '"><img src="' + image.media.m + '"/></li>'); }); $('#page > #imagesList').append(items.join'')); nImg += items.length; }); $('#page > #numImages').html(nImg); if(nImg < 1000) setTimeout("getData();", 1000); // Get more data in 1s } $(document).ready(function() { getData(); }); 

Finally, in my AppDelegate, I configured sharedURLCache as written in this post :

 // 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]; 

But, despite the fact that this should happen when I open the tools and run the application that checks the selection, the memory continues to grow when we upload images (up to 20 MB), instead of compressing about 4-6 MB, as I expected if UIWebView will use this sharedURLCache to cache downloaded images.

Instruments evolution after we reach 1000 images

Does anyone know what I'm doing wrong? Am I misunderstood something? Am I loading the page incorrectly? Does UIWebView use a different cache for images uploaded to JS?

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 ... if used at all. I do not want to have an application that uses a UIWebView that can allocate memory without control.

+6
source share
1 answer

What did you do with it at the end? I noticed that your *sharedCache initWithMemoryCapacity has a diskPath argument that is nil .

From apple doco:

In iOS, the path is the name of the application’s subdirectory by default cache directory in which to store the cache on disk (a subdirectory is created if it does not exist).

Check out this link with two bits of Labs on how to use the cache: http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/

Configure Shared Cache

First of all, let's set up the cache when the application starts (before any requests are made) so that we can control the amount of memory used. In your UIApplicationDelegate:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { int cacheSizeMemory = 4*1024*1024; // 4MB int cacheSizeDisk = 32*1024*1024; // 32MB NSURLCache *sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease]; [NSURLCache setSharedURLCache:sharedCache]; ... 

The above configuration of the 4 MB in-memory cache with 32 MB disk cache. The disk cache will be located in the default iOS cache directory in a subdirectory named nsurlcache.

Response to memory warnings

The most common cause of crashes observed in applications using web representations is expelled because it does not release enough memory when a warning about the presence of memory arrives. When your application receives a memory warning, you must clear the shared cache to free up the Memory. Do this in your UIApplicationDelegate:

 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { [[NSURLCache sharedURLCache] removeAllCachedResponses]; } 

Now, when you run your application in the profiler, you should see that the memory usage is smoothed, and if you trigger a memory warning in the simulator, you should see a memory usage drop.

+1
source

All Articles