UIWebView taking lots of memory

2019-02-06 11:07发布

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

9条回答
Luminary・发光体
2楼-- · 2019-02-06 11:32

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-06 11:32

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-02-06 11:38

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!

查看更多
做自己的国王
5楼-- · 2019-02-06 11:44
  • 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.
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-06 11:44

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.

查看更多
乱世女痞
7楼-- · 2019-02-06 11:46

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.

查看更多
登录 后发表回答