UIWebView memory-related issues in use

Source: Internet
Author: User
Tags blank page

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.