Memory Management in iOS
Memory Management iOS
In this blog we will walk through very important thing which all iOS developer should always need to take care while developing apps for same.
We will see few topics related to this thing which is below :-
- What is the term memory management?
- What may happen if we don't do memory management in iOS apps?
- Why do we need to do memory management?
- How to do memory management,i.e what all posibilities are there through which we can take care of memory in apps?
What is the term memory management?
Memory management is to reduce the memory footprint of a program by controlling the lifetime of all its objects.So whenever we are creating objects we need to take care of ownership as long as it has to be that means till what extent we have to use of these objects but no longer.Before Xcode 4.2 we had to do all these things manually like if we allocate any object we need to release that object after use i.e we had to take care of retain, release, and autorelease but Xcode 4.2 and in future version they introduced one nice feature ARC which autometically take care of memory management that mean we can say it takes care of object ownership by itself.
What may happen if we don't do memory management in iOS apps?
AS simple as that app might be getting crash.
Why do we need to do memory management?
Ofcource we will not allow our apps to get crash so we need to take care of memory which is going to be consumed by app.
What is the reason of crash ?
Not freeing data that is no longer in use causes memory leaks. A memory leak is where allocated memory is not freed, even though it is never used again. Leaks cause your application to use ever-increasing amounts of memory, which in turn may result in poor system performance or (in iOS) your application being terminated.
How to do memory management,i.e what all posibilities are there through which we can take care of memory in apps?
Here first we will see the memory management rule then will focus on ARC feature i.e how to do memory management using ARC.
Memory management rule:-
- We own the objects we create and we have to subsequently release them when they are no longer needed.
- Retain can be used to gain ownership of an object that we did not create. We have release these objects too when it's not needed.
- Don't release the objects that we don't own.
Memory management using ARC:-
In this case you don't have to relase objects manually.Whenever we create project it gives the option to check ARC.If you check this then it will take care of object ownership autometically that means object will no longer if don't have need.
Note: You can analyze the usage of memory with the help of Xcode tool Instruments. It includes tools like activity monitor, Allocations, leaks, zombies and so on.