DAMM108
DAMM108

Reputation: 960

UIWebView taking lots of memory

In my app when i load UIWebView with any Website url the memory jumps from 30mb to around 140mb. I am using ARC

here is the image for the same
and when dismissing the UIWebViewController[Viewcontroller which contains UIWebView], it doesnt releases the memory. Can any body help me how to solve this memory issues as well as please also provide me pointers of memory bestpractices in ARC

For loading the webpage :-

NSURL *nsurl=[NSURL URLWithString:self.url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];

FOr Dismissing the Viewcontroller:-

[self dismissViewControllerAnimated:YES completion:^{
    webview.delegate=nil;
    webview=nil;
}];

Thanks in Advance :)

Upvotes: 12

Views: 16269

Answers (9)

rv7284
rv7284

Reputation: 1102

I was having the same problem in my application. I would recommend that you should use WKWebView. WKWebView was introduced with iOS 8. You can read more about it here apple documentation

The memory issue is definitely solved. You will get all the functionality from webview and some more(i.e. progress). Hope this helps

Upvotes: 1

JimmyChang
JimmyChang

Reputation: 101

If you want to reduce the memory usage of UIWerview immediately, you can try:

1.

[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

or

2.

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

or

3.

[webview loadData:nil MIMEType:nil textEncodingName:nil baseURL:nil];

It worked to me.

Upvotes: 0

MrHaze
MrHaze

Reputation: 3996

Don't listen to them,

Its not a problem, phones can handle all that memory usage, try using an MKMapView and watch your memory usage soar.

In your simulator, try putting through a low memory warning cmd+shift+m, and see what happens, you can't expect all web views to run at the same memory usage, also, you have give a chance for your phone to buffer it all out.

iPhones can handle all of that, so you shouldn't worry much about it.

But try this anyway:

The behaviour of UIWebViews changes if you uncheck "Detects Links" in the nib file, so try toggling that and see what happens.

Upvotes: 3

Avt
Avt

Reputation: 17043

There is no sense to search for a problem until you are sure that problem exists.

There are two possibilities why memory usage is so high:

1.UIWebView is not deallocated.

2.It is some system caching or some specific iOS memory processing algorithm.

In second case you can do nothing. To check first case you can try to use instruments or just subclass UIWebView and add logging to dealloc method.

@implementation MyUIWebView

- (void)dealloc
{
    NSLog(@"MyUIWebView deallocated!");
}

@end

Do not forget to change UIWebView to MyUIWebView in your code.

And only if you will be sure that UIWebView is not deallocated you should start to search for strong pointers, retain circles and so on.

Upvotes: 2

E. Rivera
E. Rivera

Reputation: 10938

Just some things you should check:

  • A controller has a strong reference to its view, and this view retains all the subviews as long as you keep them inside the view hierarchy, so any additional properties should be weak, including your webView property.
  • When using ARC there's no need to "nil" everything. ARC will nil all its strong references at dealloc automatically.
  • No need to do anything with weak references neither. Moreover on iOS 5+ weak references become nil automatically if the referenced object gets deallocated somewhere else.

I think that most likely your whole view controller is getting retained somewhere, and thus its view and the webView.

Make sure the you only keep weak references to the controller and let it be retained by a container or presenting controller so it gets released automatically as long as it gets popped or dismissed.

Upvotes: 1

TinyMonk
TinyMonk

Reputation: 204

wow, today we also meet this problem. when we try to load a web page which contain lots pictures or a huge web page, it's easy to crash. we also find the memory used nearly 200M.

finally we find that we could remove the memory cache of web view

[[NSURLCache sharedURLCache] removeAllCachedResponses];

you can try it when you receive memory warning. or you can call it in the dealloc method.

if it still have some problems, try to limit the memory cache size by call

[[NSURLCache sharedURLCache] setMemoryCapacity:size]

good luck!

Upvotes: 6

Sam
Sam

Reputation: 2579

I think you forgot to remove the UIWebView from the view hierarchy. This would cause an extra retain count and prevent the memory from being reclaimed when you nil'd out the property.

Upvotes: 1

Nitin Alabur
Nitin Alabur

Reputation: 5812

add

if (webView.isLoading){
    webView.stopLoading;
}
webView.delegate=nil;

before this line

[self dismissViewControllerAnimated:YES completion...]

nil ing your webView should be taken care of by ARC. Don't refer to the web view in the completion block.

Upvotes: 1

rounak
rounak

Reputation: 9387

  • Use Instruments Allocation tab to see the number of instances of your UIWebViewController live in memory. (Cmd + I in Xcode, let Instruments open, select Allocations, and type UIWebViewController in the search bar)
  • If there's more than what you expect it to be, lookout for a retain cycle.
  • Try to use weak references within blocks instead of self.
  • Make sure you aren't holding a strong reference to the UIWebViewController object at more than one place.
  • Put a breakpoint/NSLog in the dealloc method your viewController to see if it is being called.

Upvotes: 2

Related Questions