Knowledgebase

Swift Memory Management Print

  • appletechnology, apple, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Reference counting in practice.

WHAT THE MODEL IS

References counted automatically, with memory released when the count reaches zero.

WHAT IT AVOIDS

A garbage collector, and its pauses.

WHAT IT REQUIRES ATTENTION TO

Reference cycles.

WHAT A CYCLE IS

Two objects holding strong references to each other, so neither is released.

WHERE THEY OCCUR

Closures capturing the object that owns them Delegate relationships Parent and child references

WHAT THE TWO NON-OWNING KINDS ARE

One becoming nothing when the object is released One assuming the object outlives it, crashing otherwise

WHAT TO PREFER

The safer one, unless you are certain.

WHAT TO DO IN CLOSURES

Capture the owning object weakly, and check it still exists.

WHAT VALUE TYPES AVOID

All of this, since they are not reference counted.

WHY THAT IS A REASON TO PREFER THEM

Fewer opportunities for cycles.

WHAT A LEAK LOOKS LIKE

Memory rising as screens are opened and closed, never falling.

WHAT TO USE TO FIND IT

The memory graph, showing what retains an object.

WHAT TO TEST

Opening and closing a screen repeatedly.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot