【发布时间】:2013-03-24 11:46:38
【问题描述】:
我正在观看 WWDC 2010 视频,该视频涉及高级内存分析(session 311):
在视频的 45:00 左右,性能工程师讨论了如何处理您的应用已加载到 RAM 中的“驻留脏内存”。工程师建议,为了响应内存警告,您的应用程序应该清除它。工程师将他的自定义类“flush”方法粘贴到didReceiveMemoryWarning 中,一切都很好,但代码并没有真正提供任何如何释放内存的示例。
我的问题是 - 如何刷新“Image IO”使用的大块脏内存?:
这里有大约 74 mb 的内存,只是闲置(现在将近 6 分钟),等待有人将其返回到 iOS6。它什么也没有发生。由于它不会自行消失,我需要知道如何将其返回到 iOS。
这些块似乎源自这样的代码和(可能是其他与图像相关的操作)。
UIImage *screenshot = nil;
@autoreleasepool {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(iPhoneRetinaIconSize, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(iPhoneRetinaIconSize);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
问题在于,有大量内存存在,加载到 RAM 中,在应用崩溃之前无法返回到操作系统。
对于webview相关的脏内存,我发现这可能行得通:
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
// Dispose of any resources that can be recreated.
}
是否有 UIImage、CALayer 或 UIGraphics 的等价物?
【问题讨论】:
-
据我了解,UIImage 会自动执行此操作,但仅适用于使用
imageNamed加载的图像,因此您要确保在从捆绑包加载时使用该风格。
标签: objective-c memory-management ios6 memory-profiling