Memory management in Swift(Heap, Stack, ARC)

  1. MRC : manual reference counting
  2. ARC: automatic reference counting

What is MRC?

What is ARC?

What are Object’s Lifetime?

  1. Allocation: Takes memory from a stack or heap.
  2. Initialization: init code runs.
  3. Usage: The object is used
  4. Deinitialization: deinit code runs.
  5. Deallocation: Returns memory to a stack or heap.

What are memory management issues?

  1. Freeing or overwriting data when object is in use. It causes crash or data corruption.
  2. Not freeing up the object if it is not in use. It leads memory leak.
  3. App crashes

What is memory leak?

What are main rules in memory management?

  1. when we create/own the object, subsequently release when they are no longer used again.
  2. Use retain count to release and retain the object in memory
  3. Don’t release object if you don’t own that object.

How object is retained and released in memory?

What is retain cycle/Reference Cycles?

How can you fix retain cycle?

Strong Reference Cycle in Closures:

How can we identify memory leaks?

Heaps and Stacks


  • Static in memory and allocation happens only during compile time.
  • stack is LIFO(Last in first out) data structure
  • very fast access
  • When a function is called, all local instances in that function will be pushed on to the current stack. And once the function has returned, all instances will have been removed from the stack.
  • Data stored in the stack is only there temporarily until the function exits and causes all memory on the stack to be automatically deallocated.
  • Each “scope” in your app (like the inner contents of a method) will provide the amount of memory it needs.
  • stack is not used with objects that change in size.
  • Each thread has its own stack
  • Stacks store value types, such as structs and enums.
  • If the size of your value type can be determined during compile time, or if your value type doesn’t recursively contains / is not contained by a reference type, then it will require stack allocation.
  • Value type doesn’t increases retain count. But If your value type contains inner references, copying it will require increasing the reference count of it’s children instead.


  • Dynamic in memory and allocation happens during runtime.
  • Values can be referenced at any time through a memory address.
  • no limit on memory size
  • slower access
  • When the process requests a certain amount of memory, the heap will search for a memory address that fulfils this request and return it to the process.
  • When the memory is not being used anymore, the process must tell the heap to free that section of memory.
  • It requires thread safety.
  • heap is shared with everybody
  • If the size of your value type cannot be determined during compile time (because of a protocol/generic requirement), or if your value type recursively contains / is contained by a reference type (remember that closures are also reference types), then it will require heap allocation.
  • class is stored in heap memory.

Interview tricky questions?

Thank you for reading




iOS Developer in walmart

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

5 Essential Spring Boot Features

Quick Glimpse At BSCView’s Features

“Just make it work” is a dangerous mindset.

Creating a Setup Phase with Subtests: How to Handle OAuth 2 Access Tokens using the Runscope API

What Does Dark Mode’s “supported-color-schemes” Actually Do? 🤔

Using watch to monitor output change for a command

Coding Competitions

FastComet vs BlueHost ? Detailed Comparison between them ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manasa M P

Manasa M P

iOS Developer in walmart

More from Medium

Stretchy Header Collection View with images slider

👾 Apollo Integration Fun on iOS

Making a Cocoapod in Swift with an Objective-C .framework dependency

So you want to do mock testing in XCTest?