ios - 异步 NSData 仍在内存中,没有缓存

标签 ios memory memory-management asynchronous nsdata

我的应用内存有问题。

当我启动它时,我的应用程序的文档和数据是 212KB

在我推送 ViewControler A 之后,我在 TableView 中加载了大量来自 Facebook 的异步图像:

    UIButton *friendPhoto = [UIButton buttonWithType:UIButtonTypeCustom];
    [friendPhoto addTarget:self
                    action:@selector(friendPhotoWasClicked:)
          forControlEvents:UIControlEventTouchUpInside];
    [friendPhoto setFrame:CGRectMake(x, photoMargeY, photoSize, photoSize)];

    // GET IMAGE ASYNC
    NSURL *imageURL = [NSURL URLWithString:faceURL];
    [friendPhoto setImage:[UIImage imageNamed:@"default"]
                     forState:UIControlStateNormal];
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
            NSData *data = [NSData dataWithContentsOfURL:imageURL];
            UIImage *image = [UIImage imageWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                [friendPhoto setImage:image
                             forState:UIControlStateNormal];
            });
        });
    //}
    [cell.contentView addSubview:friendPhoto];

并且工作完美!此时,文档和数据10MB

但是在弹出 ViewController 并在 NSLog 中看到为 ViewController 调用 dealloc 之后,我的应用程序仍然有 10MB。我不会缓存图像。

为什么还有 10MB?我该如何解决?

最佳答案

检查数据和图像的所有保留/释放可能会有所帮助。

如果您需要查看某个对象的保留、释放和自动释放发生在何处,请使用工具:

在 instruments 中运行,在 Allocations 中将“Record reference counts”设置为 on(您必须停止记录才能设置该选项)。导致问题代码运行,停止记录,搜索感兴趣的 ivar,向下钻取,您将能够看到所有保留、释放和自动释放发生的位置。

enter image description here

关于ios - 异步 NSData 仍在内存中,没有缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14679208/

相关文章:

ios - [<__NSCFDictionary 0x7fdf8a926250> valueForUndefinedKey :]: this class is not key value coding-compliant for the key HName

ios - 我可以获得取消自动更新订阅的收据吗?

java - jvm可以配置得体贴一些,与其他进程共享计算资源(CPU和RAM)吗?

ios - 在apple store上发布需要ipad版本吗?

c++ - 为变量存储设计内存块

c - 程序如何在内存中运行以及操作系统处理内存的方式

sql-server - 如何达到每行 8060 个字节和每个(varchar、nvarchar)值 8000 个字节的限制?

windows - Windows x86 堆栈中的堆栈是如何定义的?

iphone - 将指针设置为 nil,objective-c

ios - 带有快速下载按钮的视频播放器