UIWebView占用大量内存

DAM*_*108 12 objective-c uiwebview ipad ios automatic-ref-counting

在我的应用程序中,当我加载UIWebView任何网站URL时,内存从30mb跳到140mb左右.我正在使用ARC

这是相同的图像
当解雇UIWebViewController[ Viewcontroller包含UIWebView]时,它不会释放内存.任何人都可以帮我解决这个内存问题以及请提供内存最佳实践的指针ARC

加载网页: -

NSURL *nsurl=[NSURL URLWithString:self.url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];
Run Code Online (Sandbox Code Playgroud)

解雇Viewcontroller: -

[self dismissViewControllerAnimated:YES completion:^{
    webview.delegate=nil;
    webview=nil;
}];
Run Code Online (Sandbox Code Playgroud)

提前致谢 :)

Tin*_*onk 6

哇,今天我们也遇到了这个问题.当我们尝试加载包含大量图片或巨大网页的网页时,很容易崩溃.我们还发现内存使用了近200M.

最后我们发现我们可以删除Web视图的内存缓存

[[NSURLCache sharedURLCache] removeAllCachedResponses];
Run Code Online (Sandbox Code Playgroud)

当你收到记忆警告时,你可以尝试一下.或者你可以在dealloc方法中调用它.

如果仍有问题,请尝试通过调用限制内存缓存大小

[[NSURLCache sharedURLCache] setMemoryCapacity:size]
Run Code Online (Sandbox Code Playgroud)

祝好运!


rou*_*nak 2

  • UIWebViewController使用“仪器分配”选项卡查看内存中实时实例的数量。(Xcode中的Cmd + I,让Instruments打开,选择Allocations,然后在搜索栏中输入UIWebViewController)
  • 如果超出了您的预期,请注意保留周期。
  • 尝试在块内使用弱引用而不是 self。
  • 确保您没有UIWebViewController在多个地方持有对该对象的强引用。
  • 在 viewController 的 dealloc 方法中放置一个断点/NSLog 以查看它是否被调用。