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
    71.5K+ Learners
    4.56  (5319)
    Factorial Program in Python
    1 hrs
    Beginner
    3.1K+ Learners
    4.56  (98)
    Palindrome Program in C
    1 hrs
    Beginner
    3.4K+ Learners
    4.42  (147)
    PHP for Beginners
    2 hrs
    Beginner
    46.4K+ Learners
    4.5  (3865)
    Inferential Statistics
    1 hrs
    Beginner
    4.1K+ Learners
    4.54  (226)
    Catia Basics
    2 hrs
    Beginner
    18.3K+ Learners
    4.5  (1503)
    Circular Queue
    1 hrs
    Beginner
    3.1K+ Learners
    4.5  (213)
    Python Stack
    2 hrs
    Beginner
    5.9K+ Learners
    4.46  (308)
    AI and Big Data in IOT
    1 hrs
    Intermediate
    10.2K+ Learners
    4.48  (1167)
    Selenium Basics
    1 hrs
    Beginner
    25.5K+ Learners
    4.43  (1941)