0

我正在跟踪我的 iOS 应用程序上的内存泄漏,并且我有一个奇怪的泄漏导致我的应用程序崩溃......负责的框架是:CGImageMergeXMPPropsWhithLegacyProps。在某些时候,我的应用收到了内存警告...

我正在像这样从 ALAsset 创建 UIImage :

    ALAsset *asset = [assetsArray objectAtIndex:index];
    UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];

你知道如何解决这个问题吗?

4

1 回答 1

3

希望这有帮助。我也使用 ALAsset,但遇到内存警告。我仍在为我的应用程序搜索我的解决方案...崩溃可能是因为 iOS 释放了您的视图或对象,因为内存警告。因此,也许防止内存警告很重要。将峰值内存保持在 30MB 以下。我遇到了 ipad2 50MB 的内存警告,但没有从 iphone4 得到它。总之,越低越好。首先,您可以使用仪器或以下代码测量内存。在记录代码中测量内存更容易。1.注册一个定时器,重复上报内存使用情况,这样可以看到内存使用的峰值。另一方面,我不知道为什么这个功能会逐渐增加内存。2.《iPhone Programming The Big Nerd Ranch Guide》一书中说iOS可能有24MB的图形内存” 过度使用图形内存通常是应用程序收到内存不足警告的原因。Apple 建议您不要使用超过 24 MB 的图形内存。对于 iPhone 屏幕大小的图像,使用的内存量超过半兆字节。每个 UIView、图像、Core Animation 层以及可以在屏幕上显示的任何其他内容都会占用分配的 24 MB 空间。(Apple 不建议对 NSString 等其他类型的数据设置任何最大值。)”因此,请查看图形内存使用情况。屏幕上可以显示的任何其他内容都会占用分配的 24 MB。(Apple 不建议对 NSString 等其他类型的数据设置任何最大值。)”因此,请查看图形内存使用情况。屏幕上可以显示的任何其他内容都会占用分配的 24 MB。(Apple 不建议对 NSString 等其他类型的数据设置任何最大值。)”因此,请查看图形内存使用情况。

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);
} else {
    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");
    return 0;
}

/* Stats in bytes */
natural_t mem_free = vm_stat.free_count * pagesize;
return mem_free;
}
于 2012-11-13T13:52:14.613 回答