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
    70.8K+ Learners
    4.55  (5117)
    Factorial Program in Python
    1 hrs
    Beginner
    3.1K+ Learners
    4.56  (97)
    Palindrome Program in C
    1 hrs
    Beginner
    3.3K+ Learners
    4.44  (144)
    PHP for Beginners
    2 hrs
    Beginner
    45.4K+ Learners
    4.52  (3348)
    Inferential Statistics
    1 hrs
    Beginner
    4.1K+ Learners
    4.54  (201)
    Catia Basics
    2 hrs
    Beginner
    18.1K+ Learners
    4.52  (1440)
    Circular Queue
    1 hrs
    Beginner
    3.1K+ Learners
    4.5  (208)
    Python Stack
    2 hrs
    Beginner
    5.7K+ Learners
    4.44  (264)
    AI and Big Data in IOT
    1 hrs
    Intermediate
    10K+ Learners
    4.47  (1068)
    Selenium Basics
    1 hrs
    Beginner
    25.2K+ Learners
    4.43  (1871)