iOS

iOS Memory Management

iOS Memory Management

In today's technological age, you can't conceive a technological advancement without memory. Memory refers to the hardware that is utilised to store data on the device. The iPhone has two options in terms of data storage: the disc and the Random Access Memory (RAM). However, when an app is run on an iPhone, a file containing all of the executable instructions is loaded into the RAM.

Because all of our classes' objects are stored in a heap, the app requires some RAM to keep it running. The process of maintaining this heap memory is known as memory management. It contains heap object life cycle management, which ensures that objects are removed when no longer required, allowing memory to be saved. On the other hand, the reference types are allocated on the heap memory in Swift, although the value types are not.

Maintaining the heap memory is critical since our objects are large enough to take up a significant amount of memory if they aren't cleared. If the application continues to run out of memory, it may crash.

Automatic Reference Counting (ARC) in Swift

Automatic Reference Counting (ARC) is a smarter approach to managing memory (ARC). When you don't need the class instances, it automatically frees up their memory. As we've already mentioned in this article, Automatic Reference Counting (ARC) only applies to class instances. In Swift, Structure and Enumerations are Value types; hence their instances are not counted.

When an instance is created in our app code, ARC allocates a memory block to hold information about it. The type of the instance is included and the value of any saved properties associated with the instance.

When the instance is no longer needed, ARC frees up memory. In this case, the RAM allocated to that instance is released and used for other reasons. This also ensures that they don't take up unneeded memory space when the instances aren't in use.

HOWEVER, if ARC deallocates an instance that is still in use, you will not recover any of its attributes or methods. However, if you try to do so, the program will crash.

In addition, you must ensure that instances are not deallocated while in use. ARC keeps track of how many properties, constants, and variables are currently referring to each class instance for this reason. ARC does not deallocate it as long as there is at least one active reference to the instance. To accomplish this, it creates a strong reference to the instance anytime it is referenced by a property, constant, or variable. This reference is known as a strong reference because it will prevent that instance from being deallocated as long as the strong reference exists.

Top course recommendations for you

    Excel Tips and Tricks
    1 hrs
    Beginner
    58.8K+ Learners
    4.62  (2936)
    Factorial Program in Python
    1 hrs
    Beginner
    2.7K+ Learners
    4.64  (76)
    Palindrome Program in C
    1 hrs
    Beginner
    2.8K+ Learners
    4.38  (106)
    PHP for Beginners
    2 hrs
    Beginner
    37.7K+ Learners
    4.54  (2193)
    Inferential Statistics
    1 hrs
    Beginner
    3.3K+ Learners
    4.64  (58)
    Catia Basics
    2 hrs
    Beginner
    14.5K+ Learners
    4.52  (971)
    Circular Queue
    1 hrs
    Beginner
    3K+ Learners
    4.52  (195)
    Python Stack
    2 hrs
    Beginner
    3.8K+ Learners
    4.52  (94)
    AI and Big Data in IOT
    1 hrs
    Intermediate
    7.8K+ Learners
    4.5  (495)
    Selenium Basics
    1 hrs
    Beginner
    20.9K+ Learners
    4.41  (1193)