我在UIWebview中加載URL並且它工作正常。但是在加載Web請求期間它消耗的內存太多了。每當我在UIWebview中加載一些URL時,內存就會從30mb到95mb,並基於在UIWebview中點擊的每個鏈接,它仍然在增加,達到180MB等。我使用了一些代碼來移除UIWebview中的內存。但沒有任何好處。我已經完成了Xcode中的Analyze內存管理,但沒有泄漏,我也檢查分配和泄漏。它只是創建一個UIWebview的實例。由於UIWebview中的加載URL導致的內存快速增加ios
這是我的代碼:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease];
[NSURLCache setSharedURLCache:sharedCache];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
ViewController.h
@屬性(非原子,強)的UIWebView *的WebView;
ViewController.m @synthesize webview;
-(void)viewdidLoad
{
NSString *urlString = @"some url";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview=[uiwebview alloc]init];
[self.webview loadRequest:request];
}
/*的WebView代表
-(void)viewWillDisappear:(Bool)animated
{
self.webview=nil;
[self.webview removefromSuperView]
[self.webview loadHtmlString:@"" baseUrl:nil]
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
我面對這個問題了幾個days.Please給我一些替代或方法去除UIWebviewmemory。
不過內存leaking.The內存仍然去除的UIWebView的委託後一樣。 – 2014-12-05 16:14:33
請發表您的調整後的代碼,以便我們可以看到潛在的剩餘錯誤 – dogsgod 2014-12-05 16:54:52