Search code examples
iosobjective-cmemory-managementuiwebview

UIWebView not freeing the memory on loading PDF files


I am loading the pdf file (sizes: 15 MB) in UIWebView.

// If it is found, show it
[self.webView loadData:pdfData
              MIMEType:@"application/pdf"
      textEncodingName:@"UTF-8"
               baseURL:nil];

Problem is when I load the file many times the RAM usage goes higher and app crashesh at certain point. The occupied ram is not getting free when I pop the view controller.


Solution

  • just add three lines in your code and than check

    -(void) dealloc
    {
        [self.webview cleanForDealloc];
        self.webview = nil;
        [super dealloc];
    
    }
    

    Update:

    Called below method when I remove the view controller.

    -(void)webViewUnload {
    
        [self.webView loadHTMLString:@"" baseURL:nil];
        [self.webView stopLoading];
        [self.webView setDelegate:nil];
        [self.webView removeFromSuperview];
    
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        [[NSURLCache sharedURLCache] setDiskCapacity:0];
        [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    }