Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Memory Management

Memory Management

Lightening talk done at NSLondon about Memory Management. Covering many of the common mistakes people have with memory management using ARC and how to avoid. Briefly covering the runtime support for ARC.

Related blog post: http://kylefuller.co.uk/posts/memory-management-arc/

Kyle Fuller

October 24, 2013
Tweet

More Decks by Kyle Fuller

Other Decks in Programming

Transcript

  1. Memory Management

    View Slide

  2. -[NSObject retainCount]

    View Slide

  3. Retainable Object
    Pointers

    View Slide

  4. Object Pointers
    id
    Class
    NSObject
    __attribute__((NSObject))

    View Slide

  5. __attribute__((NSObject))

    View Slide

  6. dispatch_queue_t

    View Slide

  7. @property (nonatomic, strong)
    dispatch_queue_t queue;

    View Slide

  8. Ownership
    Qualification

    View Slide

  9. __unsafe_unretained

    View Slide

  10. __strong

    View Slide

  11. __autoreleasing

    View Slide

  12. __weak

    View Slide

  13. id objc_storeWeak(id *object, id value)

    View Slide

  14. objc_clear_deallocating

    View Slide

  15. objc_arc_weak_unavailable

    View Slide

  16. Property Declarations
    assign __unsafe_unretained
    copy -copy + __strong
    retain __strong
    unsafe_unretained __unsafe_unretained
    weak __weak

    View Slide

  17. Blocks

    View Slide

  18. `strong` vs `copy`

    View Slide

  19. Always use copy

    View Slide

  20. @property (nonatomic, copy)

    View Slide

  21. dispatch_block_t block = ^{
    NSLog(@"Hello World!");
    };
    !
    NSArray *blocks = [[NSArray alloc] initWithObject:[block copy]];

    View Slide

  22. Retain Cycle

    View Slide

  23. - (void)startOperation {
    NSOperation *operation = [[NSOperation alloc] init];
    !
    [operation setCompletionBlock:^{
    NSLog(@"Completion for %@", operation);
    }];
    }

    View Slide

  24. Operation Completion Handler

    View Slide

  25. - (void)startOperation {
    NSOperation *operation = [[NSOperation alloc] init];
    __weak NSOperation *weakOperation = operation;
    !
    [operation setCompletionBlock:^{
    NSLog(@"Completion for %@", weakOperation);
    }];
    }

    View Slide

  26. The Dealloc Problem

    View Slide

  27. Releasing UIKit objects
    in background threads

    View Slide

  28. CoreFoundation is not
    subject to ARC

    View Slide

  29. CFRelease(stringRef);

    View Slide

  30. UIColor *whiteColor = [UIColor whiteColor];
    CGColorRef whiteRef = [whiteColor CGColor];
    !
    // Crash when using whiteRef

    View Slide

  31. UIColor *whiteColor = [UIColor whiteColor];
    CGColorRef whiteRef = CGRetain([whiteColor CGColor]);
    !
    // Use whiteRef
    !
    CGRelease(whiteRef);

    View Slide

  32. Exceptions

    View Slide

  33. -fobjc-arc-exceptions

    View Slide

  34. Fast Enumeration

    View Slide

  35. for (const __strong NSObject *object in objects) {
    NSLog(@”Object: %@”, object);
    }

    View Slide

  36. Faster Enumeration

    View Slide

  37. for (const __unsafe_unretained NSObject *object in objects) {
    NSLog(@”Object: %@”, object);
    }

    View Slide

  38. Does this actually have
    any real world benefits?

    View Slide

  39. View Slide

  40. KFNumber

    View Slide

  41. Tagged Pointer

    View Slide

  42. View Slide

  43. Twitter: @kylefuller
    GitHub: kylef
    !
    http://kylefuller.co.uk/

    View Slide