iOS内存管理

2018-12-12  本文已影响44人  KeepOnline

内存管理相关面试题

1.使用CADisplayLink、NSTimer有什么注意点?
2.介绍下内存的几大区域
3.讲一下你对 iOS 内存管理的理解
4.ARC 都帮我们做了什么?
5.weak 指针的实现原理
6.autorelease 在什么时机会被释放
7.方法里有局部对象,出了方法后会立即释放吗?


CADisplayLine、NSTimer使用注意:

CADisplayLine、NSTimer会对target产生强引用,如果target对他们也产生强引用,那么就会造成循环引用

解决方案:
  1. 使用block
    __weak typeof(self) weakSelf = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterVal:2.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakSelf test];
    }];
  1. 使用代理对象(NSProxy)
    + (instancetype)proxyWithTarget:(id)target {
        KPLTimerProxy *proxy = [self alloc];
        proxy.target = target;
        return proxy;
    }
    
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
        return [self.target methodSignatureForSelector:sel];
    }
    
    - (void)forwardInvocation:(NSInvocation *)invocation {
        [invocation invokeWithTarget:self.target];
    }
    KPLTimerProxy *proxy = [KPLTimerProxy proxyWithTarget:self];
    self.timer = [NSTimer scheduledTimerWithATimeInterval:2.0 target:proxy selector:@selector(test) userInfo:nil repeats:YES];

GCD定时器:

1.NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时
2.GCD的定时器会更加准时

    // 创建一个定时器
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    // 设置时间(start是几秒后开始执行,interval是时间间隔)
    dispatch_source_set_timer(timer, dispatch_timer(DISPATCH_TIME_NOW, (int64_t)(start * NSEC_PER_SEC)), (uint64_t)(interval * NSEC_PER_SEC), 0);
    // 设置回调
    dispatch_source_set_event_handler(tiemr, ^{
        
    });
    // 启动定时器
    dispatch_resume(timer);

iOS程序的内存布局:

iOS内存布局.png

Tagged Pointer


面试题

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    for (int i = 0; i < 1000; i++) {
        dispatch_async(queue, ^{
            self.name = [NSString stringWithFormat:@"abkcikiifjjdj"];
        });
    }
    dispatch_queue_t queue = dispatch_get_global_queue;
    dispatch_async(queue, ^{
        self.name = [NSString stringWithFormat:@"abc"];
    });

name属性使用copy或者strong修饰

    - (void)setName:(NSString *)name {
        if (_name != name) {
            [_name release];    
            _name = [name copy];
        }
    }
    - (void)setName:(NSString *)name {
        if (_name != name) {
            [_name release];
            _name = [name retain];
        }
    }

代码段a执行后会出现坏内存访问导致的闪退现象:

代码段b没有问题


OC对象的内存管理


引用计数的存储

    struct SideTable {
        spinlock_t slock;
        RefcountMap refcnts;
        weak_table_t weak_table;
    }

dealloc

    void *objc_destructInstance(id obj) {
        bool cxx = obj->hasCxxDtor();
        bool assoc = obj->hasAssociatedObjects();
        
        if (cxx) object_cxxDestruct(obj); // 清除成员变量
        if (assoc) _object_remove_assocations(obj); 
        obj->clearDeallocating();  // 将指向当前对象的弱指针置为nil
    
    }

自动释放池

源码分析

    magic_t const magic;
    id *next;
    pthread_t const thread;
    AutoreleasePoolPage * const parent;
    AutoreleasePoolPage *child;
    uint32_t const depth;
    uint32_t hiwat;

AutoreleasePoolPage 的结构


Runloop和Autorelease

上一篇 下一篇

猜你喜欢

热点阅读