Memory control
date
May 31, 2023
slug
memory-control
status
Published
tags
memory
summary
A quick overview of how memory is managed in Swift. From the article, it will become clearer to you what improvements have occurred in the transition from Objective-C to Swift and how this has affected modern development.
type
Post
Manual Reference Counting (MRC)
When developing applications for iOS in the Objective-C language, a system of manual memory management is used. When creating objects using the
alloc
, new
, copy
, mutableCopy
, autorelease
, retain
the object reference count is incremented by 1, and when the release
method is called, it is decremented by 1.Each time an object is used in code, the number of references to it increases. When an object is no longer needed, the program must explicitly indicate this by releasing the reference to it. This is especially important when using objects with large amounts of memory, such as images or videos.
When the number of
strong
references to an object becomes zero, the object is removed from the heap. But it is worth remembering that an object can also have weak
references, which do not affect its life cycle and do not prevent it from being removed from memory.The object is deleted on the thread where the last
release
was made. If at this time another thread is still using it, this can lead to errors in the program.Automatic Reference Counting (ARC)
It's important to note that in Objective-C you must explicitly manage memory using the
retain
and release
methods when you need to increment or decrement an object's reference count. If you are using ARC, then you do not need to call these methods explicitly, as ARC automatically manages memory in your application.While the application is running, ARC continuously keeps track of the number of object references. As soon as the number of
strong
references to an object becomes zero, the object is removed from memory.Apple has comprehensive and well-written documentation for this mechanism. I strongly recommend checking it out.
Autorelese Pool
The autorelease pool was used in MRC, but with ARC the autorelease pool is only used directly for Objcs or objects marked with the
@autorelease
attribute. In Swift, the autorelease pool is used automatically.Let's say you have a room that needs to be cleaned and you need to throw out a lot of trash. You can throw away each piece of trash as soon as you're done with it, but this will take a long time and may not be very efficient if you have a lot of trash.
Instead, you can use a trash container, such as a trash bag. You put the garbage in a bag and then throw away the whole bag when it is full.
Similarly, when you create objects in Swift, you can put them in an autorelease pool, which is a kind of container for objects. Instead of releasing each object individually, you can put them in an autorelease pool and then release the entire pool when it's full or needed.
Thus, using an autorelease pool helps simplify memory management in your program, since you don't have to worry about releasing each object manually.
In Swift, an autorelease pool is created automatically when entering a
for
, while
, repeat
loop.