This article reprinted to: http://blog.csdn.net/musou_ldns/article/details/7675589 Applicationios5webkit Test Cacheios
In the process of developing an application on the IOS5 platform, it was found that the memory could not be released even if the UIWebView screen was closed after UIWebView was loaded via nsurlrequest.
As a result, the more pages accessed through UIWebView, the more memory used by the application, and the possibility of a memory warning causing the program to exit.
By investigating the relevant information on the Internet, it is found that UIWebView's memory problems have existed in the iOS4 era.
Stackflow has a number of related questions and solutions, such as emptying the cache:
-(void) Webviewdidfinishload: (UIWebView *) WebView {
New for memory cleaning
[[Nsuserdefaults Standarduserdefaults] setinteger:0 forkey:@ "Webkitcachemodelpreferencekey"];
New for Memory cleanup
[[Nsurlcache Sharedurlcache] setmemorycapacity:0];
Nsurlcache *sharedcache = [[Nsurlcache alloc] initwithmemorycapacity:0 diskcapacity:0 Diskpath:nil];
[Nsurlcache Setsharedurlcache:sharedcache];
}
At the end of the screen, let UIWebView load a blank page:
[WebView loadhtmlstring:@ "" Baseurl:nil];
[WebView stoploading];
[WebView Setdelegate:nil];
[WebView Removefromsuperview];
[[Nsurlcache Sharedurlcache] removeallcachedresponses];
But after a real-machine test, it was found to have no effect.
The opinion of most developers on the web is that the current UIWebView memory problem is in WebKit, and developers still do not have a good solution.
Then found another disguised solution, since it is not possible to prevent the continuous increase in memory consumption, it is controlled within a certain range and emptied in a timely manner.
The specific way is as follows:
In the Uiapplicationdelegate
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Add the following code to the Launchoptions method:
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];
Processing when adding memory warnings at the same time:
-(void) applicationdidreceivememorywarning: (uiapplication *) Application {
[[Nsurlcache Sharedurlcache] removeallcachedresponses];
}
Real-machine testing does reduce the amount of system memory, but the memory footprint of the application itself does not change much.
The above survey results are for reference only.