面试宝点

内存管理

2020-08-25  本文已影响0人  鼬殿

CADisplayLink、NSTimer使用注意

@property (strong, nonatomic) CADisplayLink *link;
@property (strong, nonatomic) NSTimer *timer;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 保证调用频率和屏幕的刷帧频率一致,60FPS
    self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(linkTest)];
    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self  selector:@selector(timerTest) userInfo:nil repeats:YES];
}

- (void)timerTest
{
    NSLog(@"%s", __func__);
}

- (void)linkTest
{
    NSLog(@"%s", __func__);
}

- (void)dealloc
{
    NSLog(@"%s", __func__);
    [self.link invalidate];
    [self.timer invalidate];
}

CADisplayLink、NSTimer会对target产生强引用,如果target又对它们产生强引用,那么就会引发循环引用, dealloc方法并不会走

解决方案:
Timer用block方案

__weak typeof(self) weakSelf = self;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
      [weakSelf timerTest];
}];

timer对block强引用,block对self弱引用

使用代理对象(NSProxy)

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[NJFProxy proxyWithTarget:self] selector:@selector(timerTest) userInfo:nil repeats:YES];
@interface NJFProxy : NSProxy
+ (instancetype)proxyWithTarget:(id)target;
@property (weak, nonatomic) id target;
@end

@implementation NJFProxy

+ (instancetype)proxyWithTarget:(id)target
{
    // NSProxy对象不需要调用init,因为它本来就没有init方法
    NJFProxy *proxy = [NJFProxy alloc];
    proxy.target = target;
    return proxy;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
    return [self.target methodSignatureForSelector:sel];
}

- (void)forwardInvocation:(NSInvocation *)invocation
{
    [invocation invokeWithTarget:self.target];
}
@end

GCD定时器

NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时
GCD定时器不受RunLoop约束

@interface NJFTimer : NSObject

+ (NSString *)execTask:(void(^)(void))task
           start:(NSTimeInterval)start
        interval:(NSTimeInterval)interval
         repeats:(BOOL)repeats
           async:(BOOL)async;

+ (NSString *)execTask:(id)target
              selector:(SEL)selector
                 start:(NSTimeInterval)start
              interval:(NSTimeInterval)interval
               repeats:(BOOL)repeats
                 async:(BOOL)async;

+ (void)cancelTask:(NSString *)name;

@end

@implementation NJFTimer

static NSMutableDictionary *timers_;
dispatch_semaphore_t semaphore_;

+ (void)initialize
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        timers_ = [NSMutableDictionary dictionary];
        semaphore_ = dispatch_semaphore_create(1);
    });
}

+ (NSString *)execTask:(void (^)(void))task start:(NSTimeInterval)start interval:(NSTimeInterval)interval repeats:(BOOL)repeats async:(BOOL)async
{
    if (!task || start < 0 || (interval <= 0 && repeats)) return nil;
    // 队列
    dispatch_queue_t queue = async ? dispatch_get_global_queue(0, 0) : dispatch_get_main_queue();
    // 创建定时器
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    // 设置时间
    dispatch_source_set_timer(timer,
                              dispatch_time(DISPATCH_TIME_NOW, start * NSEC_PER_SEC),
                              interval * NSEC_PER_SEC, 0);
    dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER);
    // 定时器的唯一标识
    NSString *name = [NSString stringWithFormat:@"%zd", timers_.count];
    // 存放到字典中
    timers_[name] = timer;
    dispatch_semaphore_signal(semaphore_);
    // 设置回调
    dispatch_source_set_event_handler(timer, ^{
        task();
        if (!repeats) { // 不重复的任务
            [self cancelTask:name];
        }
    });
    // 启动定时器
    dispatch_resume(timer);
    return name;
}

+ (NSString *)execTask:(id)target selector:(SEL)selector start:(NSTimeInterval)start interval:(NSTimeInterval)interval repeats:(BOOL)repeats async:(BOOL)async
{
    if (!target || !selector) return nil;
    return [self execTask:^{
        if ([target respondsToSelector:selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            [target performSelector:selector];
#pragma clang diagnostic pop
        }
    } start:start interval:interval repeats:repeats async:async];
}

+ (void)cancelTask:(NSString *)name
{
    if (name.length == 0) return;
    
    dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER);
    dispatch_source_t timer = timers_[name];
    if (timer) {
        dispatch_source_cancel(timer);
        [timers_ removeObjectForKey:name];
    }
    dispatch_semaphore_signal(semaphore_);
}

@end

iOS程序的内存布局

Tagged Pointer

如何判断一个指针是否为Tagged Pointer?

static inline bool 
_objc_isTaggedPointer(const void * _Nullable ptr)
{
    return ((uintptr_t)ptr & _OBJC_TAG_MASK) == _OBJC_TAG_MASK;
}

#if (TARGET_OS_OSX || TARGET_OS_IOSMAC) && __x86_64__
    // 64-bit Mac - tag bit is LSB
#   define OBJC_MSB_TAGGED_POINTERS 0
#else
    // Everything else - tag bit is MSB
#   define OBJC_MSB_TAGGED_POINTERS 1
#endif

#if OBJC_MSB_TAGGED_POINTERS
如果是iOS平台, 最高有效位是1(第64bit, 使用高位优先规则 MSB)
#   define _OBJC_TAG_MASK (1UL<<63)
#else
如果是mac平台, 最低有效位是1(低位优先规则 LSB)
#   define _OBJC_TAG_MASK 1UL
#endif

面试题

思考以下2段代码能发生什么事?有什么区别?

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

先看如下代码执行结果(ios项目)

NSString *str1 = [NSString stringWithFormat:@"abcdefghijk"];
NSString *str2 = [NSString stringWithFormat:@"abc"];
NSLog(@"%@ %@", [str1 class], [str2 class]);
NSLog(@"%p %p", str1,str2);
__NSCFString NSTaggedPointerString
0x28129d540 0xf5ad71b08c1d52a1

str1内存地址很明显不够64位,高位补0 & 1 = 0,最高位不是1,所以不是Tagged Pointer,是OC对象
str2内存地址从右往左,第16位是f, 换算成二进制就是0b1111,最高位1 & 1 = 1,所以是Tagged Pointer,不是OC对象

self.name会调用如下代码,由于是异步全局并发队列,所以会有多条线程执行release操作,会造成EXC_BAD_ACCESS,坏内存访问

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

OC对象的内存管理

extern void _objc_autoreleasePoolPrint(void);

copy和mutableCopy

1、浅拷贝
浅拷贝就是对内存地址的复制,让目标对象指针和源对象指向同一片内存空间,当内存销毁的时候,指向这片内存的几个指针需要重新定义才可以使用,要不然会成为野指针。

image

浅拷贝就是拷贝指向原来对象的指针,使原对象的引用计数+1,可以理解为创建了一个指向原对象的新指针而已,并没有创建一个全新的对象
2、深拷贝
深拷贝是指拷贝对象的具体内容,而内存地址是自主分配的,拷贝结束之后,两个对象虽然存的值是相同的,但是内存地址不一样,两个对象也互不影响,互不干涉。

image

深拷贝就是拷贝出和原来仅仅是值一样,但是内存地址完全不一样的新的对象,创建后和原对象没有任何关系

深拷贝就是内容拷贝,浅拷贝就是指针拷贝

No1:可变对象的copymutableCopy方法都是深拷贝(区别完全深拷贝与单层深拷贝)
No2:不可变对象的copy方法是浅拷贝,mutableCopy方法是深拷贝。
No3:copy方法返回的对象都是不可变对象。

引用计数的存储

在64bit中,引用计数可以直接存储在优化过的isa指针中,也可能存储在SideTable类中

inline uintptr_t 
objc_object::rootRetainCount()
{
    if (isTaggedPointer()) return (uintptr_t)this;
    sidetable_lock();
    isa_t bits = LoadExclusive(&isa.bits);
    ClearExclusive(&isa.bits);
    if (bits.nonpointer) {//优化过的isa
        uintptr_t rc = 1 + bits.extra_rc;
        if (bits.has_sidetable_rc) {//引用计数不是储存在isa中,而是储存在sidetable中
            rc += sidetable_getExtraRC_nolock();
        }
        sidetable_unlock();
        return rc;
    }
    sidetable_unlock();
    return sidetable_retainCount();
}


struct SideTable {
    spinlock_t slock;
*******************************
refcnts是一个存放着对象引用计数的散列表
*******************************
    RefcountMap refcnts;
*************************************
弱引用表,当前对象的内存地址作为key,指向该对象的弱引用指针作为value
*************************************
    weak_table_t weak_table;
....
};

dealloc

当一个对象要释放时,会自动调用dealloc

- (void)dealloc {
    _objc_rootDealloc(self);
}

inline void
objc_object::rootDealloc()
{
    if (isTaggedPointer()) return;  // fixme necessary?
    if (fastpath(isa.nonpointer  &&  
                 !isa.weakly_referenced  &&  
                 !isa.has_assoc  &&  
                 !isa.has_cxx_dtor  &&  
                 !isa.has_sidetable_rc))
    {
        assert(!sidetable_present());
        free(this);
    } 
    else {
        object_dispose((id)this);
    }
}

id 
object_dispose(id obj)
{
    if (!obj) return nil;
    objc_destructInstance(obj);    
    free(obj);
    return nil;
}

void *objc_destructInstance(id obj) 
{
    if (obj) {
        // Read all of the flags at once for performance.
        bool cxx = obj->hasCxxDtor();
        bool assoc = obj->hasAssociatedObjects();
        // This order is important.
        if (cxx) object_cxxDestruct(obj);//清除成员变量
        if (assoc) _object_remove_assocations(obj);//清除关联对象
*************************************
将指向当前的对象的弱指针置为nil
*************************************
        obj->clearDeallocating();
    }
    return obj;
}

inline void 
objc_object::clearDeallocating()
{
    if (slowpath(!isa.nonpointer)) {
        // Slow path for raw pointer isa.
        sidetable_clearDeallocating();
    }
    else if (slowpath(isa.weakly_referenced  ||  isa.has_sidetable_rc)) {
        // Slow path for non-pointer isa with weak refs and/or side table data.
        clearDeallocating_slow();
    }
    assert(!sidetable_present());
}
......

自动释放池

自动释放池的主要底层数据结构是:__AtAutoreleasePool、AutoreleasePoolPage
调用了autorelease的对象最终都是通过AutoreleasePoolPage对象来管理的
源码分析

@autoreleasepool { } 

转为C++底层源码

__AtAutoreleasePool __autoreleasepool

声明了一个__AtAutoreleasePool类型的局部变量

struct __AtAutoreleasePool {
  __AtAutoreleasePool() {//构造函数,在创建结构体的时候调用
atautoreleasepoolobj = objc_autoreleasePoolPush();
}
  ~__AtAutoreleasePool() {//// 析构函数,在结构体销毁的时候调用
objc_autoreleasePoolPop(atautoreleasepoolobj);
}
  void * atautoreleasepoolobj;
};

源码https://opensource.apple.com/tarballs/objc4/

void *
objc_autoreleasePoolPush(void)
{
    return AutoreleasePoolPage::push();
}

void
objc_autoreleasePoolPop(void *ctxt)
{
    AutoreleasePoolPage::pop(ctxt);
}

static inline void *push() 
{
     id *dest;
    //判断是否是debug 环境
     if (slowpath(DebugPoolAllocation)) {
          // Each autorelease pool starts on a new pool page.
          dest = autoreleaseNewPage(POOL_BOUNDARY);
      } else {
          dest = autoreleaseFast(POOL_BOUNDARY);
      }
      ASSERT(dest == EMPTY_POOL_PLACEHOLDER || *dest == POOL_BOUNDARY);
      return dest;
}

AutoreleasePoolPage的结构

class AutoreleasePoolPage : private AutoreleasePoolPageData{
}

struct AutoreleasePoolPageData
{
    magic_t const magic;     16      检查结构体完整性
    __unsafe_unretained id *next;    8 next指针,指向最新添加的 autoreleased 对象的下个位置
    pthread_t const thread;   8  指向当前线程  用来取出自动释放池
    AutoreleasePoolPage * const parent;   8 指向父结点,第一个结点的 parent 值为nil
    AutoreleasePoolPage *child;    8  指向子结点,最后一个结点的 child 值为nil
    uint32_t const depth;  4  深度 链表链层的深度 从0开始,往后递增1
    uint32_t hiwat;   4 最大入栈数量
.....
};   56个字节

POOL_BOUNDARY 边界(哨兵对象),每创建一个@autoreleasepool { } 就会创建一个POOL_BOUNDARY

示例

extern void _objc_autoreleasePoolPrint(void);

int main(int argc, const char * argv[]) {
    @autoreleasepool { //  r1 = push()
        NSObject *obj1 = [[[NSObject alloc] init] autorelease];
        NSObject *obj2 = [[[NSObject alloc] init] autorelease];
        @autoreleasepool { // r2 = push()
            for (int i = 0; i < 3; i++) {
                NSObject *obj3 = [[[NSObject alloc] init] autorelease];
            }
            @autoreleasepool { // r3 = push()
                NSObject *obj4 = [[[NSObject alloc] init] autorelease];
                _objc_autoreleasePoolPrint();
            } // pop(r3)
        } // pop(r2)
    } // pop(r1)
    return 0;
}
objc[1602]: ##############
objc[1602]: AUTORELEASE POOLS for thread 0x1000d2dc0
objc[1602]: 9 releases pending.
objc[1602]: [0x10100b000]  ................  PAGE  (hot) (cold)
objc[1602]: [0x10100b038]  ################  POOL 0x10100b038
objc[1602]: [0x10100b040]       0x10066e1f0  NSObject
objc[1602]: [0x10100b048]       0x10066d5d0  NSObject
objc[1602]: [0x10100b050]  ################  POOL 0x10100b050
objc[1602]: [0x10100b058]       0x10066d140  NSObject
objc[1602]: [0x10100b060]       0x10066c8d0  NSObject
objc[1602]: [0x10100b068]       0x100666de0  NSObject
objc[1602]: [0x10100b070]  ################  POOL 0x10100b070
objc[1602]: [0x10100b078]       0x100665f50  NSObject
objc[1602]: ##############
Program ended with exit code: 0

每页AutoreleasePoolPage可以容纳(4096 - 56)/8 = 505 个对象,第一页会加入一个特殊的对象边界。也就是说第一页可以加 504个自己的对象,之后的都是505个。

Runloop和Autorelease

MRC环境
iOS在主线程的Runloop中注册了2个Observer
第1个Observer监听了kCFRunLoopEntry事件,会调用objc_autoreleasePoolPush()
第2个Observer
监听了kCFRunLoopBeforeWaiting事件,会调用objc_autoreleasePoolPop()、objc_autoreleasePoolPush()
监听了kCFRunLoopBeforeExit事件,会调用objc_autoreleasePoolPop()
ARC环境
出了方法后,ARC会对方法里的局部对象调用release,对象会被立即释放

上一篇下一篇

猜你喜欢

热点阅读