简体   繁体   English

释放ARC / non-ARC中的对象会使内存处于使用状态

[英]Releasing objects in ARC/non-ARC leaves memory in use

I have a general issue with memory management. 我对内存管理有一个一般性的问题。 I can create an object with the following code, fill it with data, then clean up and release it, but even after the object is released the memory it used is still in use. 我可以使用以下代码创建一个对象,将其填充数据,然后清理并释放它,但是即使释放该对象之后,它所使用的内存仍在使用中。

- (void)viewDidLoad {
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    NSBundle *bundle1 = [NSBundle mainBundle];
    NSString *path = [bundle1 pathForResource:@"Oxford Latin Dictionary - Optimized" ofType:@"pdf"];
    NSURL *pathURL = [NSURL fileURLWithPath:path];
    PDFObject* pdfObject = [[PDFObject alloc] initWithURL:pathURL withCachedPages:25 startAtPage:1 withFrame:self.view.frame];
    [pdfObject readPdfAtPage:1];
    [pdfObject generateThumbnails:self.view.frame.size.width/10];
    [pdfObject cleanThumbnailsAndSubviews];
    [pdfObject clearMemory];
    [pdfObject release];
}

The program uses about 9MB before creating the pdfObject ( PDFObject* pdfObject = [[PDFObject alloc] init... ), it uses about 23MB when that object is initialized and set up ( [pdfObject generateThumbnails:self.view.frame.size.width/10]; ), then the program still uses about 23MB after all the objects within pdfObject are released and pdfObject itself is released. 该程序在创建pdfObject之前使用大约9MB的空间( PDFObject* pdfObject = [[PDFObject alloc] init... ),在初始化和设置该对象时使用大约[pdfObject generateThumbnails:self.view.frame.size.width/10];空间( [pdfObject generateThumbnails:self.view.frame.size.width/10]; ),则在释放pdfObject中的所有对象并且释放pdfObject本身之后,程序仍使用约23MB的空间。 I have the same problem with ARC turned on and using NSObject = nil to force a release of the object. 我在打开ARC并使用NSObject = nil强制释放对象时遇到相同的问题。 This eventually causes a crash when I try to create and destroy too many of these objects. 当我尝试创建和销毁太多这些对象时,最终会导致崩溃。

I must be missing some simple part of objective c memory management, but I thought I was following good practices (ie if you create an object you must eventually destroy it). 我肯定缺少目标c内存管理的一些简单部分,但我认为我遵循的是良好的做法(即,如果创建对象,则最终必须销毁它)。 Coming from a JAVA background doesn't help things. 来自JAVA背景并没有帮助。

Use ARC. 使用ARC。 Using manual reference counting in 2014 is just silly. 在2014年使用人工参考计数是很愚蠢的。 It makes life harder without good reason. 没有合理的理由,这会使生活更加艰难。

Given that you are using manual reference counting, your code looks reasonable. 鉴于您正在使用手动引用计数,因此您的代码看起来很合理。 You create a number of objects, but they all appear to be autoreleased temporary objects except the PDFObject that you alloc/init, and then release at the end. 您创建了许多对象,但是它们似乎都是自动释放的临时对象,除了您分配/初始化的PDFObject之外,然后最后释放。

My guess is that the PDFObject is doing image caching internally. 我的猜测是PDFObject在内部进行图像缓存。 That would cause your app's memory footprint to rise, but not in a bad way. 那会导致您的应用程序的内存占用量增加,但不会变坏。 If memory pressure increases, the system will flush cached images before taking more serious steps like sending you memory warnings or killing your app. 如果内存压力增加,系统将在采取更严重的步骤(例如向您发送内存警告或终止您的应用程序)之前刷新缓存的图像。

It's also possible that the PDFObject class has memory leaks in it, or that it's doing it's own caching on top of system-based image caching. PDFObject类也可能在其中存在内存泄漏,或者它是在基于系统的图像缓存之上进行自己的缓存。

You may want to use the memory analysis Instruments to take a look at the objects that are adding to your app's memory footprint. 您可能需要使用内存分析工具来查看添加到应用程序内存占用量中的对象。 Explaining how to do that is beyond the scope of a forum post however. 但是,如何做到这一点超出了论坛的讨论范围。 There have been WWDC session videos in the past about this, and there are also quite a few blog posts and online tutorials out there that explain how to use the instruments tool to figure out why your app's memory use is growing. 过去有WWDC会话视频,与此相关的还有很多博客文章和在线教程,它们解释了如何使用工具工具来弄清楚为什么应用程序的内存使用量不断增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM