Weak、Strong、assign 和 autorelease + 1道面试题


一、weak、strong、assign的理解

1. OC 对象用 strong,为什么连线的ui控件却用weak?

controller → view →  view.subViews →  imageView →  强引用
controller →  imageView →  弱引用
controller →  imageView 这个位置换成 strong 也可以,但是不建议,如果一个对象被多个对象强引用,当 这多个对象中有一个对象忘记释放,那么该对象也不能释放。
ARC环境指定编译文件不使用arc  - fno - objc -arc 。
补充:
让程序兼容ARC和非ARC部分:
转变为非 ARC   -fno-objc-arc  
转变为 ARC    -f-objc-arc 




2.Weak 和 assign的区别

weak 对象释放后指向0地址;
assign 对象释放后指向地址不变,成为野指针;


二、自动释放池

1.MRC内存管理原则:
// 谁申请 , 谁释放。
// 遇到  alloc  / copy / retain, 都需要添加  release  或  autorelease。

2. autorelease:
// 添加一个自动释放的标记 , 会延迟自动释放。
// 当一个 autorelease 的对象超出了作用域之后, 会被添加到最近的自动释放池中。
// 当自动释放池被释放之前, 会像池中所有的对象发送 release 消息, 释放池中所有的对象。 

3. iOS开发中的内存管理
(1)在iOS开发中,并没有JAVA或C#中的垃圾回收机制
(2)使用ARC开发,只是在编译时,编译器会根据代码结构自动添加了 retain releaseautorelease

4. 自动释放池的工作原理
(1)标记为 autorelease的对象在出了作用域范围后,会 被添加到最近 一次创建的 自动 放池
(2)当自动释放池 被销毁耗尽时,会向自动释放池中的所有对象发送 release消息

5. 自动释放池和主线程 

原文:
The Application Kit creates an
autorelease pool on themain thread at the beginning of every cycle of the event loop, and drains it at theend, thereby releasing any autoreleased objects generatedwhile processing an event. If you use the Application Kit, youtherefore typically don’t have to create your own pools. If your applicationcreates a lot of temporary autoreleased objects within the event loop, however, it may be beneficial to create “local” autorelease pools to help to minimize the peak memory footprint. 
在主线程消息循环开始的时候创建了自动释放池,在消息循环结束的时候清掉自动释放池

6.什么时候使用自动释放池 
If youwrite a loop that creates many temporary objects.
You may use an
autorelease pool block inside the loop to dispose of those objects before the nextiteration. Using an autorelease pool block in the loop helps to reduce the maximum memory footprint of the application. 
循环中创建了许多临时对象,在循环里面使用自动释放池,用来减少高内存占用。


If youspawn a secondary thread.
You must create your own
autorelease pool block as soon as the thread begins executing; otherwise, your applicationwill leak objects. (See Autorelease Pool Blocks and Threads for details.)

 开启子线程的时候要自己创建自动释放池,否则可能会发生内存泄露。


7.自动释放池常见面试代码

面试题:

int largeNumber = 999*9999;

for  ( int i = 0; i < largeNumber; ++i ) {

    NSString *str = @"Hello World";

    str = [str stringByAppendingFormat:@" - %d", i ];

    str = [str uppercaseString];

    NSLog(@"%@", str);

}

问:以上代码存在什么样的问题?如果循环的次数非常大时,应该如何修改?

答:

    int largeNumber = 999*9999;

    for (int i = 0; i < largeNumber; ++i) {

        @autoreleasepool {

            NSString *str = @"Hello World";

            str = [str stringByAppendingFormat:@" - %d", i];

            str = [str uppercaseString];

            NSLog(@"%@", str);

        }   

    }











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值