Memory Management

1. Explain ARC

ARC stands for auto reference counting, it's used to determine whether a memory block should or should not be deallocated.

When an object is created, its reference count begins with 1. This reference count can increase or decrease during its lifecycle. At last, when the reference count reeaches 0, the object is deallocated from memory.

2. What is strong, weak, assign, unowned reference and copy?

  • Strong variables will increase the reference count
  • Weak and unowned variables do not retain the object
  • Weak reference will be automatically set to nil
  • Weak reference is used where there is possibility for that reference to become nil at some point during its lifetime.
  • Unowned reference is used when the other instance has the same lifetime or a longer lifetime.
  • Copy means that the property will use a strong reference because it must hold on to the new object it creates.
  • Assign is used for primitive types, the compiler will create the setter using simple assign operation.

Note: There's no assign and copy in swift.

3. What is a retain cycle?

  • Retain cycle occurs when two objects strongly reference each other.
  • As a result, the reference count of either object will never reach zero and deallocated from memory.

4. Explain Retain cycle in blocks and closure

  • Closures and blocks will retain the objects within, so if we have a class with a closure variable, and that variable happends to reference a property or method in the owning object, there would be a retain cycle. The solution is to define a weak version of self.
  • dispatch_async per se will NOT cause a retain cycle

5. What is a memory leak?

  • A memory leak commonly occurs when an object is allocated in such a way that when it is no longer in use or needed, it is not released.
  • Memory that can't be accessed again.
  • Retain cycle will cause memory leak.

6. How to debug memory leaks?

  • Memory Graph Debugger
  • Static analyzer
  • Using Instrument Memory Leak tool

7. Why do you generally create a weak reference when using self in a block?

  • Sometimes it is necessary it capture self in a block such as when defining a callback block.
  • However, since blocks maintain strong references to any captured objects including self, this may lead to a strong reference cycle and memory leak.

8. What is the difference between a stack vs a heap?

  • A stack is a region of memory where data is added or removed in a last-in-first-out (LIFO) order.
  • Meanwhile the heap is memory set aside for dynamic allocation.
  • Unlike the stack, you can allocate a block at any time and free it at anytime.

Note: In Objective-C, all objects are always allocated on the heap, or at least should be treated as if on the heap.

9. What is @dynamic in Objective-C ? We use dynamic for subclasses of NSManagedObject. @dynamic tells the compiler that getter and setters are implemented somewhere else

How many different annotations available in Objective-C

results matching ""

    No results matching ""