Anúncio

ARC.pptx

31 de Mar de 2023
Anúncio

Mais conteúdo relacionado

Destaque(20)

Anúncio

ARC.pptx

  1. ARC (Automatic Retain Count) Michael Jonathan
  2. Definition It is a memory management technique which provided reference counting of the objects for the programming languages.
  3. Principle of cocoa memory management • When object brought into existence it must set the memory and freed up when object goes out of existence. • Object should go out from memory when no object exist have pointer to it. • Object without a pointer is useless, it’s occupying memory, but no object can have access to it, this is called Memory leak • Multiple objects can have a same pointer to the very same object • Object A and object B have a pointer to object C, if object A tell object C to go out of existence now, object C will left with a pointer to nothing, a pointer whose object has been destroy behind the pointer called dangling pointer, if object B use dangling pointer to send message it will crash
  4. Retain Count • To prevent memory dangling pointers and memory leakage, there are policy of manual memory management by number, maintained by every reference type object, called retain count. • Any object can increment and decrement an object retain count, as long as the retain count is positive. • No object have power to tell other object to be destroyed. • When an object retain count reach 0 it the object will be destroyed automatically.
  5. Rules of Cocoa Memory Management Consider 3 objects: Manny, Moe and Jack. Manny and Moe will managing Jack memory, if it managed correctly, it will go out of existence correctly, everything will work fine when they follow this rules: • If Manny or Moe directly calling jack initializer, then the initializer increments Jack’s retain count • If Manny or Moe makes a copy of jack, by calling mutableCopy or any copy method, the copy method increments jack’s retain count • If Manny or Moe acquires a reference to jack, and need jack to persist, long enough to work with Jack in code, or long enough to be the value of an instance property, then he himself increments jack’s retain count • After Manny or Moe no needs his reference anymore to Jack, before letting go it decrements jack retain count equal to balance exactly they performed, when jack retain count become zero it will automatically release jack
  6. Rules of Cocoa Memory Management A general way to understand the rule is to think in terms of ownership, if an object has created, copied or retained other object, the first object has asserted an ownership to the other object, and the first object have it own responsibility to increment and decrement other object retain count correctly.
  7. Weak vs Unowned Weak Unowned • ARC doesn’t retain the object assigned to it, but ARC keeps track of all weak references • ARC doesn’t retain the object assigned to it, and ARC take its hands off completely • When object going to destroy ARC sneaks in and assign nil to the reference, it’s why weak must var • When object is destroy, it can left a dangling pointer and can make crash when calling dangling pointer • Weak reference is safer than unowned • Used only when object referred to will not go out of existence
  8. Debugging Memory Leak Memory Debugging Instrument Print in deinit
  9. Memory Debugging Notes: For a maximal result try it on real device
Anúncio