我的iOS应用内存使用率很高但没有内存泄漏。如何减少内存使用量。
使用Instruments,我发现我的应用程序最大值为90MB,在发生内存警告之前,其他内存被释放,然后在其余部分使用时保持在55-65MB左右。
我觉得55-65MB太高了吧?
因为,仪器没有发现任何泄漏。我该怎么做才能减少内存使用量?
我浏览了今年的WWDC视频,但是我理解的东西(这是我的第一个iOS应用程序),它主要涉及泄漏处理。
一些可能有用的信息:
VM:ImageIO_GIF_Data 30.35MB Live Bytes | 115生活| 300瞬态| 136.12 MB总字节数
VM:MappedFile 36.04 MB Live Bytes | 16生活| 11瞬态| 36.09 MB总字节数
所有其他内容都低于1MB
我的应用程序从互联网下载大约30个GIF文件,我使用SDWebImage,我只保存图像的URL,SDWebImage完成剩下的工作。 :P
提前致谢,
来自iOS内存管理第一计时器
再次感谢您的帮助
答案 0 :(得分:4)
您说您正在使用表格视图。尽管单元格是自动重用的,但这样很容易出错并创建太多对象。 1常见错误是在cellForRowAtIndexPath方法中分配对象(例如UIImageView),因为这意味着每次重用一个单元时,都会向其添加一个新的UIImageView并保留旧的UIImageView。因此,请仔细检查您的cellForRowAtIndexPath方法中发生了什么。
答案 1 :(得分:1)
我建议您使用仪器和快照分析。 bbum在his blog写了一篇关于它的文章。
以下是快速概述:
Mark Heap
;这是你的基线。Mark Heap
。如果您看到内存稳定增长,您可以深入了解快照并查看分配的所有对象。这应该会为您减少内存占用提供良好的开端。
答案 2 :(得分:1)
我决定添加完整的内存保存代码,如果你使用的是GIF文件,修改UIImage scale方法(在这里找到它,一个Stackoverflow)。正如SD Image中所说的 GangstaGraham 存在方法sd_animatedImageByScalingAndCroppingToSize
@interface UIImage (Scaling)
-(UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize;
-(UIImage*) croppedImageWithRect: (CGRect) rect;
@end
@implementation UIImage (Scaling)
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
targetSize.height *= 2.0f;
targetSize.width *= 2.0f;
}
}
NSUInteger width = targetSize.width;
NSUInteger height = targetSize.height;
UIImage *newImage = [self resizedImageWithMinimumSize: CGSizeMake (width, height)];
return [newImage croppedImageWithRect: CGRectMake ((newImage.size.width - width) / 2, (newImage.size.height - height) / 2, width, height)];
}
-(CGImageRef)CGImageWithCorrectOrientation
{
if (self.imageOrientation == UIImageOrientationDown) {
//retaining because caller expects to own the reference
CGImageRetain([self CGImage]);
return [self CGImage];
}
UIGraphicsBeginImageContext(self.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (self.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (context, 90 * M_PI/180);
} else if (self.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, -90 * M_PI/180);
} else if (self.imageOrientation == UIImageOrientationUp) {
CGContextRotateCTM (context, 180 * M_PI/180);
}
[self drawAtPoint:CGPointMake(0, 0)];
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIGraphicsEndImageContext();
return cgImage;
}
-(UIImage*)resizedImageWithMinimumSize:(CGSize)size
{
CGImageRef imgRef = [self CGImageWithCorrectOrientation];
CGFloat original_width = CGImageGetWidth(imgRef);
CGFloat original_height = CGImageGetHeight(imgRef);
CGFloat width_ratio = size.width / original_width;
CGFloat height_ratio = size.height / original_height;
CGFloat scale_ratio = width_ratio > height_ratio ? width_ratio : height_ratio;
CGImageRelease(imgRef);
return [self drawImageInBounds: CGRectMake(0, 0, round(original_width * scale_ratio), round(original_height * scale_ratio))];
}
-(UIImage*)drawImageInBounds:(CGRect)bounds
{
UIGraphicsBeginImageContext(bounds.size);
[self drawInRect: bounds];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
-(UIImage*)croppedImageWithRect:(CGRect)rect
{
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.size.width, self.size.height);
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
[self drawInRect:drawRect];
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return subImage;
}
-(UIImage *) resizableImageWithCapInsets2: (UIEdgeInsets) inset
{
if ([self respondsToSelector:@selector(resizableImageWithCapInsets:resizingMode:)])
{
return [self resizableImageWithCapInsets:inset resizingMode:UIImageResizingModeStretch];
}
else
{
float left = (self.size.width-2)/2;//The middle points rarely vary anyway
float top = (self.size.height-2)/2;
return [self stretchableImageWithLeftCapWidth:left topCapHeight:top];
}
}
@end
和UIImageView:
#import <SDWebImage/SDImageCache.h>
@implementation UIImageView (Scaling)
-(void)setImageWithURL:(NSURL*)url scaleToSize:(BOOL)scale
{
if(url.absoluteString.length < 10) return;
if(!scale){
[self setImageWithURL:url];
return;
}
__block UIImageView* selfimg = self;
__block NSString* prevKey = SPRINTF(@"%@_%ix%i", url.absoluteString, (int)self.frame.size.width, (int)self.frame.size.height);
__block UIImage* prevImage = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^ {
prevImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:prevKey];
if(prevImage){
dispatch_async(dispatch_get_main_queue(), ^ {
[self setImage:prevImage];
});
}else{
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:SDWebImageDownloaderFILOQueueMode progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if(error){
[selfimg setImageWithURL:url scaleToSize:scale];
}else{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^ {
prevImage = [image imageByScalingProportionallyToSize:self.frame.size];
if(finished)
[[SDImageCache sharedImageCache] storeImage:prevImage forKey:prevKey];
dispatch_async(dispatch_get_main_queue(), ^ {
[self setImage:prevImage];
});
});
}
}];
}
});
return;
}
-(void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder scaleToSize:(BOOL)scale
{
[self setImage:placeholder];
[self setImageWithURL:url scaleToSize:scale];
}
@end
答案 3 :(得分:0)
SDWebImage不会完成剩下的工作。
你需要在内存中处理更少的图像:
当UIImageView没有显示时擦除;
使用可重复使用的对象模式;
当你收到内存警告时,当然清除不可见(缓存在内存中)的图像,
为此,只需使用self.urImage = nil;
所以,好好寻找app内存保存;)