我正在跟踪我的 iOS 应用程序的内存泄漏,我有一个奇怪的泄漏导致我的应用程序崩溃......负责的框架是:CGImageMergeXMPPropsWhithLegacyProps。在某些时候,我的应用程序收到内存警告 ...
我正在像这样从 ALAsset 创建 UIImage :
ALAsset *asset = [assetsArray objectAtIndex:index];
UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
你知道如何解决这个问题吗?
希望对您有所帮助。我也用了ALAsset,遇到了内存警告。我仍在为我的应用程序搜索我的解决方案...崩溃可能是因为 iOS 由于内存警告而释放了您的 View 或对象。所以,也许防止内存警告很重要。将峰值内存保持在 30MB 以下。我遇到了 ipad2 的 50MB 内存警告,但没有从 iphone4 中获取。无论如何,越低越好。 首先,您可以测量内存通过使用仪器或以下代码。更容易测量代码中的内存以进行日志记录。1.注册一个定时器,用于重复报告内存使用情况,通过这种方式可以看到内存使用峰值。 另一方面,我不知道为什么这个函数会逐渐增加内存。2.《iPhone Programming The Big Nerd Ranch Guide》一书说iOS可能有24MB的图形内存 “过度使用图形内存通常是应用程序收到内存不足警告的原因。Apple 建议您不要使用超过 24 MB 的图形内存。对于图像iPhone屏幕的大小,使用的内存量超过半兆字节。每个 UIView、图像、Core Animation 层以及可以在屏幕上显示的任何其他内容都会占用分配的 24 MB 内存中的一部分。 (对于 NSString 等其他类型的数据,Apple 没有建议任何最大值。)” 因此,查看图形内存使用情况。
NSTimer * timeUpdateTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(reportMem) userInfo:nil repeats:TRUE];
[[NSRunLoop mainRunLoop] addTimer:timeUpdateTimer forMode:NSDefaultRunLoopMode];
-(void) reportMem{
[self report_memory1];
}
-(void) report_memory1 {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
natural_t freem =[self get_free_memory];
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use %f %f(free)", info.resident_size/1000000.0,(float)freem/1000000.0);
} khác {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
-(natural_t) get_free_memory {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
NSLog(@"Failed to fetch vm statistics");
trả về 0;
}
/* Stats in bytes */
natural_t mem_free = vm_stat.free_count * pagesize;
return mem_free;
}
Tôi là một lập trình viên xuất sắc, rất giỏi!