iOS Memory Management - IOS

What is iOS - Memory Management?

Memory management in iOS was initially non-ARC (Automatic Reference Counting), where we have to maintain and release the objects. Now, it supports ARC and we don't have to maintain and release the objects. Xcode will take care of the job automatically in compile time.

Memory Management Issues

According to Apple documentation, the two main issues in memory management are −

  • Freeing or overwriting of data that is still in use. It results in memory corruption and typically causes application crashing, or worse, corrupted user data.
  • Not freeing data that is no longer in use results in memory leaks. When allocated memory is not emptied even though it is not going to be used again, it is called as memory leak. Leaks will cause your application to make use ever-increasing amounts of memory, which in turn may cause in poor system performance or (in iOS) your application will be terminated.

Memory Management Rules

  • We own the objects we create, and we have to consequently release them whenever they are no longer required.
  • Use maintain to gain ownership of an object that you did not create. You have to release these objects also when they are not required.
  • Don't release those objects that you don't own.

Handling Memory in ARC

You don't require to use release and maintain in ARC. So, all the view controller's objects will be released when the view controller is removed. In the same way, any object’s sub-objects will be released when they are released. Note that if other classes have a strong reference to an object of a class, then the whole class will not be released. So, it is suggesthed to use weak properties for delegates.

Memory Management Tools

We can inspect the usage of memory by the help of Xcode tool instruments. It includes tools like Activity Monitor, Allocations, Leaks, Zombies, and so on.

Steps for Analyzing Memory Allocations

Step 1.Open an existing application.

Step 2.Select Product and then Profile as shown in the below screen shot.

iOS Memory Management

Step 3.Select Allocations in the next screen displayed below and select Profile.

iOS Memory Management

Step 4.We will view the allocation of memory for various objects as shown below.

Step 5 You can switch between view controllers and check if the memory is released properly or not.

iOS Memory Management

Step 6. In the same way, in place of Allocations, we can employ Activity Monitor to view the overall memory allocated for the application.

iOS Memory Management

Step 7.These tools will help us access our memory consumption and locate the places where possible leaks have been occurred.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IOS Topics