2019-09-17 02:13 来源:未知


至于iOS的UIWebView内部存储器走漏的主题材料,以前也遇上过那些标题,消除格局正是设置NSU揽胜LCache大小。因为iOS个中的 网络通讯暗中认可都以通过NSUENCORELConnection来兑现的。所以UIWebView内部报纸发表也是经过NSU本田CR-VLConnection来下载网页能源的。

宣称变量:isFirstLoad  // 保存是不是是第贰回加载,推断shouldStartLoadWithRequest是还是不是要举办加载当前恳请


- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

标题参照他事他说加以考察:转自:iOS7 UIWebView内部存款和储蓄器败露难题消除方法




int cacheSizeMemory = 1*1024*1024; // 4MB

if (isFirstLoad) {


int cacheSizeDisk = 5*1024*1024; // 32MB

CGRect frame = self.webView.frame;


NSURLCache *sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease];

[self.webView removeFromSuperview];


[NSURLCache setSharedURLCache:sharedCache];

UIWebView *webviews = [[UIWebView alloc] initWithFrame:frame];



webviews.delegate  =self;



[self.view addSubview:webviews];


- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application

[webviews loadRequest:request];

- webViewDidFinishLoad:(UIWebView *)webView{


self.webView =webviews;

// 幸免内存狂升

[[NSURLCache sharedURLCache] removeAllCachedResponses];

//首先创造JSContext 对象(此处通过当前webView的键获取到jscontext)

[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];


JSContext *context = [webviews valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//本身丰裕的,最早的小说没有关联。


//public 为听从JSExport 左券的类

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//本身丰富的,原来的文章未有涉嫌。

_webView.delegate = nil;

ocTojs_Public *public = [[ocTojs_Public alloc] init];

[[NSUserDefaults standardUserDefaults] synchronize];

[_webView loadHTMLString:@"" baseURL:nil];

public.obj = self;


[_webView stopLoading];

context[@"DownLoad"] = public;


[_webView removeFromSuperview];

isFirstLoad = NO;

UIWebView Secrets - Part1 - Memory Leaks on XmlhttprequestOCT 4TH, 2010My first blog post on iphone subject reveal a big memory bug when using UIWebView component. This is the component to display some HTML content in an iphone interface. UIWebView object has a lot of differents issues and I’m going to highlight the biggest of them. Actually, all XMLHttpRequests used in javascript code are fully leaking!!! I mean when you do a request that retrieve 100ko of data, your memory used grow up for 100ko! This bug is not always active, but almost always. In fact the trigger to enable it is to simply open one link in your UIWebView. For example, clicking on alink.

[[NSURLCache sharedURLCache] removeAllCachedResponses];

return NO;

But let’s look at a memory usage graph while we execute this simple test application: memory usage graph

[_webView release];


Create the UIWebView object


isFirstLoad = YES;

Load a local HTML test file


return YES;

Execute 3 XMLHttpRequest to google.com, notice how the memory is freed three times after each request!



Trigger the leak by opening a page that redirect back to our test file



Execute the same 3 XMLHttpRequest and look how much memory is used and totally leaked :/

var xmlhttp = new XMLHttpRequest();

加载uiwebview 内部存款和储蓄器泄漏导致内部存款和储蓄器大涨的难题(不过成效非常的小,最佳化解办法ios8以上用wkwebview):

We clean the HTML document with document.body.innerHTML=”; (sometimes free some memory, when we have a lot of DOM objects)

xmlhttp.onreadystatechange = function() {

-(void)webViewDidFinishLoad:(UIWebView *)webView{

release the UIWebView (almost no memory freed, next post is going to analyse that)

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

[ShowActionView hiddenLoadingView:self.view];

Test Application

// Do whatever you want with the result

[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];

So, to sum up, usually, when you execute this Javascript in a UIWebView:


[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自个儿充分的,原版的书文未有关联。

var xmlhttp = new XMLHttpRequest();


[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自个儿加上的,原来的小说未有提到。

xmlhttp.onreadystatechange = function() {

xmlhttp.open("GET", "http://your.domain/your.request/...", true);

[[NSUserDefaults standardUserDefaults] synchronize];

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {



// Do whatever you want with the result



[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];


[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自个儿丰盛的,原来的文章没有涉及。

xmlhttp.open("GET", "...", true);

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自身丰裕的,原版的书文未有涉嫌。


[[NSUserDefaults standardUserDefaults] synchronize];

Your are going to have a big memory usage and leak a lot of data!


But there is a hack to solve this problem: revert what is done when you open a link.

In fact, the key property which leads to this leak is the WebKitCacheModelPreferenceKey application setting. And when you open a link in a UIWebView, this property is automatically set to the value "1". So, the solution is to set it back to 0 everytime you open a link. You may easily do this by adding a UIWebViewDelegate to your UIWebView :

- webViewDidFinishLoad:(UIWebView *)webView {

[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];


So are you going to have much less crash due to "Low Memory" :)