下面关于Objective-C内存管理的描述错误的是
A 当使用ARC来管理内存时,代码中不可以出现autorelease
B autoreleasepool 在 drain 的时候会释放在其中分配的对象
C 当使用ARC来管理内存时,在线程中大量分配对象而不用autoreleasepool则可能会造成内存泄露
D 在使用ARC的项目中不能使用NSZone
= =。。
网上看到的,参考答案为A.
我觉得选C来的~
不知道A错在哪里了?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
A的错误在于在使用ARC时,编译器会禁止你使用autorelease,而由编译器帮你添加,就像retain,release一样。但是你可以使用__autoreleasing来指定变量,使它加入autoreleasepool
A 和 D 基本是相同的,但实际上
NSZone *zone = NSDefaultMallocZone();这句代码是可以编译运行的。
你看的参考答案不对。
ARC 下,不能使用 autorelease 进行编程,但是可以使用
@autoreleasepool。它的作用是降低内存占用。Each thread in a Cocoa application maintains its own stack of autorelease pool blocks.
这句话只陈述了一个事实“Cocoa 应用的每一个线程维护了它自己的自动释放池块的栈”
If you are writing a Foundation-only program or if you detach a thread, you need to create your own autorelease pool block.
如果你在编写 Foundation-only 应用 或者 自己 detach 一个线程,你需要创建自己的自动释放池块。
If your application or thread is long-lived and potentially generates a lot of autoreleased objects, you should use autorelease pool blocks (like AppKit and UIKit do on the main thread); otherwise, autoreleased objects accumulate and your memory footprint grows. If your detached thread does not make Cocoa calls, you do not need to use an autorelease pool block.
如果你的应用或者线程长时间存活,并可能产生大量的自动释放的对象;你应该自动自动释放池块;否则,自动释放的对象会积累并占用你的内存。
如果你创建的线程没有调用 Cacoa,你不需要使用自动释放池块。
你自己开辟的新的线程,里面的内存就得自己去管理的,只有在主线程的中runloop中才会自动帮你加autoreleasePush()跟autoreleasePop().
因为ARC是编译器特性,而不是iOS运行时特性,更不是其他语言中的垃圾收集器。
所以这就意味这它只能处理在编译时就确定的内存管理,所用的机制就是引用计数。
换句话来讲,他的内存释放不是强制的,比如内存相互引用,动态引用等会导致引用计数不会立刻置0,所以这个时候显式释放是有必要的。