www.2527.com_澳门新葡8455手机版_新京葡娱乐场网址_
做最好的网站

UIWebView内部存款和储蓄器泄露难题消除方法,关

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

宣示:本人不是原创,本身只是难点一举成功的苦力。

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

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

缓慢解决格局:iOS8的用WKWebView,7的用UIWebView加上边的片段优化。

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

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

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

{

{

一篇国外的Blog文章:

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

难点汇报:每一遍加载webView时候内部存款和储蓄器剧增,导致程序反应开始变慢。

[NSURLCache setSharedURLCache:sharedCache];

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

难点出现的缘故:

}

webviews.delegate  =self;

斩草除根思路:先百度,后Google,实在可怜问二弟!

与此同不常候在吸收接纳内部存款和储蓄器警告的时候,清除缓存内容。

[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"];//本身丰裕的,最早的小说没有关联。

以及在释放UIWebView的时候

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

总的来看一篇海外的Blog文章,找到本人想要的答案。

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

Html个中的js代码会挑起内部存款和储蓄器走漏的难题。

参考:

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) {

xmlhttp.send();

}

// Do whatever you want with the result

消除那些主题素材的点子是在webViewDidFinishLoad方法中装置如下:

}

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

};

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

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

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

xmlhttp.send();

[[NSUserDefaults standardUserDefaults] synchronize];

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

关于NSUserDefaults的另类用法还大概有诸如设置UserAgent也得以由此NSUserDefaults来设置。

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" :)

TAG标签:
版权声明:本文由澳门新葡8455手机版发布于计算机编程,转载请注明出处:UIWebView内部存款和储蓄器泄露难题消除方法,关