UIWebView 占用大量内存

2023-10-22移动开发问题
5

本文介绍了UIWebView 占用大量内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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


当关闭包含 UIWebView 的 UIWebViewController[Viewcontroller] 时,它不会释放内存.任何人都可以帮助我如何解决这个内存问题,还请在 ARC


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

用于加载网页:-

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

在此先感谢 :)

推荐答案

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

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

您可以在收到内存警告时尝试.或者你可以在 dealloc 方法中调用它.

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]

祝你好运!

这篇关于UIWebView 占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7