简体   繁体   中英

Rapidly increasing memory due to load url in UIWebview ios

I am loading URL in UIWebview and it is working fine.But during loading of web request it is consuming memory too much.Whenever i load some URL in UIWebview memory is increased from 30mb to 95mb and based on each link clicked in UIWebview it is still increasing and reach to 180mb and so on.I used some code to remove memoryin UIWebview.But there is no benefit.I have done memory management by Analyze in Xcode but there are no leaks and i also check allocations and leaks.It is just creating one instance of UIWebview.

This is my code:

AppDelegate.m

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

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

ViewController.h

@property(nonatomic,strong)UIWebview *webview;

ViewController.m @synthesize webview;

-(void)viewdidLoad
{
    NSString *urlString = @"some url";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webview=[uiwebview alloc]init];
    [self.webview loadRequest:request];
}

/*webview delegates

-(void)viewWillDisappear:(Bool)animated
{
   self.webview=nil;
 [self.webview removefromSuperView]
 [self.webview loadHtmlString:@"" baseUrl:nil]
  [[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
  [[NSURLCache sharedURLCache] removeAllCachedResponses];
  for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) 
  {

      if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {

       [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
  }
}

I am facing this issue for few days.Please give me some alternative or method to remove UIWebviewmemory.

In viewWillDisapear You are setting your WebView reference to nil so the removing in the next line fails.

Try setting your reference to nil after you completed all cleanup around the WebView , ie in the last line of the method.

You probably also need to remove the delegate of the WebView (before nilling).

As dogsgod is pointed out the way OP release memory is not proper, but
there are other things which should mention for this type of question.

  • If you are developing new application I suggest you to use ARC for your project
  • If its not possible to implement ARC then follow below advice

In viewdidLoad() you are allocating object that's good but its better if you first check that is object is already allocated of not (as it may change that it is allocated before pushing it into navigation).

Sample Code

if(self.webview!=nil)
{
self.webview = nil; // release memory
}

// Code for allocating memory.

If you alloc object in viewDidLoad() then must be deallco in dealloc() method.

NOTE:
When you assign any object nil, it means now object is not point to the old location and you have no control over that object, so calling some method like [self.webview removefromSuperView] after assigning nil to webview it have no effect .


Extra note:
Please read this document for knowledge about methods like viewDidload,viewWillAppear ... etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM