Autoreleasepool and swift

Kristiina Rahkema
ITNEXT

Memory management in swift is handled with ARC (= automatic reference counting). This means that active references to objects are counted and objects are released when they aren’t referenced anymore.

In Objective-C (especially in the old days of Objective-C, before the introduction of ARC) it was necessary to retain and release objects manually. Developers needed to manage the memory of each object and mistakes in memory management could result in memory leaks or crashes.

In Swift memory management is done automatically through ARC and in most cases developers do not need to worry about managing it. There are some exceptions though. For example:

  • References to self in closures that can mess with ARC
  • Uses of Objective-C objects in swift

References to self in closures can be handled by using the keywords weak and unowned. I wrote a small blogpost about it here:

Uses of Objective-C objects in swift can lead to these objects not being correctly released. The fix for this issue is autoreleasepool.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by Kristiina Rahkema

I’m an iOS developer with an interest in app security. I’m also a PhD student and junior researcher working on static analysis for mobile applications.

Responses (2)

What are your thoughts?

Very useful. Thanks

--

Nice article!
Is there no implementation in Swift for move semantics (like in Rust) so ARC isn’t even needed in most cases?

--