WebView内存管理

Jam*_*Mac 5 webkit memory-management objective-c webview

我正在开发的应用程序保留了几个WebView,这些WebView用于为集中在应用程序周围的某些活动提供嵌入式Web浏览体验.我遇到的问题是,打开几个小时后,经过大量使用后,视图开始积累内存.我对Objective-C中的内存管理的理解是,一旦对象被完全释放(保留count = 0)并且被释放,整个应用程序使用的内存量就会减少.这似乎不适用于我的情况.

[webviewObject release];
webviewObject = nil;

webviewObject = [[self createNewViewWithName:name] retain];
Run Code Online (Sandbox Code Playgroud)

以上是我正在使用的代码.我试过释放内存而不是创建一个新实例,但没有运气.内存使用量继续增长,根据Instruments的说法,对象完全释放.我错过了什么吗?应用程序可以缓存一些如何?

Nee*_*eku 1

我在我的一个应用程序的网络视图中遇到了类似的问题。似乎 iPhone SDK 中的 Web 视图存在一些内存泄漏。当网络视图中存在大文本以及其他一些问题时,我的应用程序崩溃了。这段代码帮助我防止内存泄漏。我希望这对你也有用。

\n\n

以下是该链接中的说明:

\n\n
\n

防止与 UIWebView 相关的内存泄漏可能很困难。Apple\xe2\x80\x99s\n 关于该主题的文档很少,而且不直接,\n 仅仅是因为 UIWebView 是一个复杂的野兽,其中使用了许多底层\n 类。此 Objective-C 类别代表从各种网络博客和有关该主题的讨论中收集的想法和理论(认为是魔法而不是科学......)的集合。有关更多\n Objective-C 内存管理思想,请查看 WIJL:IOS 内存\n 管理。使用类别很简单:

\n\n

下载代码:UIWebView+Clean.zip

\n\n
    \n
  1. 将 UIWebView+Clean.h 和 .m 文件添加到您的项目中。
  2. \n
  3. 导入UIWebView+Clean.h您想要在其中使用该类别的任何文件。
  4. \n
  5. cleanForDealloc在您想要释放 UIWebView 的任何地方使用该方法:
  6. \n
\n
\n\n
[self.webView cleanForDealloc];\nself.webView = nil;\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是UIWebView+Clean.h

\n\n
/*\n\n    UIWebView+Clean.h\n\n    A simple category that performs recommended UIWebView clean before dealloc\n\n    Created by Jason Baker (jason@onejasonforsale.com) for TumbleOn on 3/18/12.\n\n    Copyright (c) 2012, Pocket Sized Giraffe, LLC\n    All rights reserved.\n\n    Redistribution and use in source and binary forms, with or without\n    modification, are permitted provided that the following conditions are met:\n\n    1. Redistributions of source code must retain the above copyright notice, this\n    list of conditions and the following disclaimer.\n    2. Redistributions in binary form must reproduce the above copyright notice,\n    this list of conditions and the following disclaimer in the documentation\n    and/or other materials provided with the distribution.\n\n    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\n    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\n    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n    The views and conclusions contained in the software and documentation are those\n    of the authors and should not be interpreted as representing official policies,\n    either expressed or implied.\n\n */\n\n#import <Foundation/Foundation.h>\n\n@interface UIWebView (clean)\n\n// performs various cleanup activities recommended for UIWebView before dealloc.\n// see comments in implementation for usage examples\n- (void) cleanForDealloc;\n\n@end\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是UIWebView+Clean.m

\n\n
/*\n\n UIWebView+Clean.h\n\n A simple category that performs recommended UIWebView clean before dealloc\n\n Created by Jason Baker (jason@onejasonforsale.com) for TumbleOn on 3/18/12.\n\n Copyright (c) 2012, Pocket Sized Giraffe, LLC\n All rights reserved.\n\n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions are met:\n\n 1. Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n 2. Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n\n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\n ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\n ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n The views and conclusions contained in the software and documentation are those\n of the authors and should not be interpreted as representing official policies,\n either expressed or implied.\n\n */\n\n#import "UIWebView+Clean.h"\n\n@implementation UIWebView (clean)\n\n/*\n\n Using this Category is easy, simply add this to the top of your file where\n you have a UIWebView:\n\n #import "UIWebView+Clean.h"\n\n Then, any time you want to throw away or deallocate a UIWebView instance, do\n the following before you throw it away:\n\n [self.webView cleanForDealloc];\n self.webView = nil;\n\n If you still have leak issues, read the notes at the bottom of this class,\n they  may help you.\n\n */\n\n\n- (void) cleanForDealloc\n{\n    /*\n\n     There are several theories and rumors about UIWebView memory leaks, and how\n     to properly handle cleaning a UIWebView instance up before deallocation. This\n     method implements several of those recommendations.\n\n     #1: Various developers believe UIWebView may not properly throw away child\n     objects & views without forcing the UIWebView to load empty content before\n     dealloc.\n\n     Source: http://stackoverflow.com/questions/648396/does-uiwebview-leak-memory\n\n     */        \n    [self loadHTMLString:@"" baseURL:nil];\n\n    /*\n\n     #2: Others claim that UIWebView\'s will leak if they are loading content\n     during dealloc.\n\n     Source: http://stackoverflow.com/questions/6124020/uiwebview-leaking\n\n     */\n    [self stopLoading];\n\n    /*\n\n     #3: Apple recommends setting the delegate to nil before deallocation:\n     "Important: Before releasing an instance of UIWebView for which you have set\n     a delegate, you must first set the UIWebView delegate property to nil before\n     disposing of the UIWebView instance. This can be done, for example, in the\n     dealloc method where you dispose of the UIWebView."\n\n     Source: UIWebViewDelegate class reference    \n\n     */\n    self.delegate = nil;\n\n\n    /*\n\n     #4: If you\'re creating multiple child views for any given view, and you\'re\n     trying to deallocate an old child, that child is pointed to by the parent\n     view, and won\'t actually deallocate until that parent view dissapears. This\n     call below ensures that you are not creating many child views that will hang\n     around until the parent view is deallocated.\n     */\n\n    [self removeFromSuperview];\n\n    /*\n\n     Further Help with UIWebView leak problems:\n\n     #1: Consider implementing the following in your UIWebViewDelegate:\n\n     - (void) webViewDidFinishLoad:(UIWebView *)webView\n     {\n        //source: http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part1-memory-leaks-on-xmlhttprequest/\n        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];\n     }\n\n     #2: If you can, avoid returning NO in your UIWebViewDelegate here:\n\n     - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType\n     {\n        //this source says don\'t do this: http://stackoverflow.com/questions/6421813/lots-of-uiwebview-memory-leaks\n        //return NO;\n        return YES;\n     }\n\n     #3: Some leaks appear to be fixed in IOS 4.1\n     Source: http://stackoverflow.com/questions/3857519/memory-leak-while-using-uiwebview-load-request-in-ios4-0\n\n     #4: When you create your UIWebImageView, disable link detection if possible:        \n\n     webView.dataDetectorTypes = UIDataDetectorTypeNone;\n\n     (This is also the "Detect Links" checkbox on a UIWebView in Interfacte Builder.)\n\n     Sources:\n     http://www.iphonedevsdk.com/forum/iphone-sdk-development/46260-how-free-memory-after-uiwebview.html\n     http://www.iphonedevsdk.com/forum/iphone-sdk-development/29795-uiwebview-how-do-i-stop-detecting-links.html\n     http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/\n\n     #5: Consider cleaning the NSURLCache every so often:\n\n     [[NSURLCache sharedURLCache] removeAllCachedResponses];\n     [[NSURLCache sharedURLCache] setDiskCapacity:0];\n     [[NSURLCache sharedURLCache] setMemoryCapacity:0];\n\n     Source: http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/\n\n     Be careful with this, as it may kill cache objects for currently executing URL\n     requests for your application, if you can\'t cleanly clear the whole cache in\n     your app in some place where you expect zero URLRequest to be executing, use\n     the following instead after you are done with each request (note that you won\'t\n     be able to do this w/ UIWebView\'s internal request objects..):\n\n     [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];\n\n     Source: http://stackoverflow.com/questions/6542114/clearing-a-webviews-cache-for-local-files  \n\n     */\n}\n\n@end\n
Run Code Online (Sandbox Code Playgroud)\n