很多的开发人员会将注意力放在业务模块,以至于疏忽了内存部分的优化,刚开始看不出什么问题,当上线之后或者项目慢慢庞大的时候,会出现各种各样的问题,因为内存问题相对于一般的问题来说,比较难以定位和查找,所以需要我们时时注意,在每一个小功能点做好内存控制。
我总结了一些在项目开发过程中,所做的一些优化以及需要注意的点。
// 标记1 NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1 target:self selector:@selector(timerFire) userInfo:nil repeats:YES]; // 标记2 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; // 标记3 self.timer = timer;
__weak typeof(self) weakSelf = self; [self doSomeBlockJob:^{ }];
imageWithContentsOfFile:
创建图片的图片管理方式.NSString *path = [NSBundle.mainBundle pathForResource:@"image@2x" type:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:path];
UIImage *image = [UIImage imageNamed:@"image"];
[SDImageCache sharedImageCache].shouldDecompressImages = NO; [SDWebImageDownloader sharedDownloader].shouldDecompressImages = NO;
[[SDImageCache sharedImageCache] clearMemory];
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { [[SDImageCache sharedImageCache] clearMemory]; }
#import <sys/stat.h> struct stat statbuf; const char *cpath = [filePath fileSystemRepresentation]; if (cpath && stat(cpath, &statbuf) == 0) { NSNumber *fileSize = [NSNumber numberWithUnsignedLongLong:statbuf.st_size]; NSDate *modificationDate = [NSDate dateWithTimeIntervalSince1970:statbuf.st_mtime]; NSDate *creationDate = [NSDate dateWithTimeIntervalSince1970:statbuf.st_ctime]; }