One of the major things in any mobile application development is a memory management and performance enhancement,
Here is an instant guide or list of few crucial points w.r.t. enhancing our apps memory management, in turn performance optimizations in our iOS application, which I came across in my daily life and spent lot of time in fixing it, thought it would be great or it may save someones valuable time and energy if I share my note on this regard,
Here we go,
1) Better to make all IBOutlet objects as __weak reference in ARC environment. Except for those from file’s owner to top-level objects in a xib(nib or a storyboard scene) file which should be strong. Outlets that you create typically should be weak by default.
2) Instead of calling init constructor method every time when pushing/navigating to a view controller/view call init only once and until and unless the viewController object reference exists in memory use the same reference to navigate.
Ex:- Suppose we are navigating from Home screen to a DetailViewController then create an iVar of DetailViewController globally and in your push method check for this iVar reference in memory and navigate like,
detailVCObj = [[DetailViewController alloc] initWithNibName:@”DetailViewController” bundle:nil];//if object reference is not their in memory then only alloc and initilize
[self.navigationController pushViewController:detailVCObj animated:YES];
and suppose if you want to set some property values before pushing a view controller then create your custom getter and setter methods for those properties and call those methods before pushing view whenever you are following the above protocol.
By doing this(by following this protocol) it will optimize memory consumption during runtime. Follow this protocol even in ARC enabled projects.
3) In ARC enabled projects, assign nil to all re-creatable objects and iVars after making use of these objects, so as to handle memory in an optimized manner.
In MRC (Manual Reference Counting) type of projects release re-creatable objects and iVars and then assign nil, after making use of these objects.
Assigning nil is equivalent to release and dereference in ARC.
a) In ARC enabled environment, myiVar = nil;
b) In MRC environment,
[myiVar release];//releasing object
myiVar = nil;//dereferencing the object
4) Avoid using images up to the maximum extent in your app, and try to achieve the design by using colors wherever and whenever it is possible for you.
Since this will optimize both time complexity (load time for image is more compared to a UIColor) and space complexity (size of .ipa file will be reduced drastically if we avoid images).
If you are using images then based on your requirement and particular instance which UIImage method to use? refer this link
5) Make use of Singleton design pattern in your application wherever it is required and can be used.
Ex:- Suppose if you are calling some of the methods of a class frequently in your app, like a database model class in your app will be called frequently for either to insert or to fetch or to update different tables and its entities. Make this class as a singleton class so that even if you make n number of calls to this class methods single object will manipulate the functionality.
For more information on important design patterns in iOS refer this link
6) Whenever you are making a core data fetch call use includesPropertyValues where ever possible, refer this link for further details on this API.
7) Keep track of temporary memory buffers and reuse it, and don’t keep unused memory, free it up as soon as you are done using it.
8) iOS will automatically kills the background processes and apps which consumes more memory and causes memory pressure during low memory conditions, its every programmer responsibility to handle low memory conditions inside our app as-well.
During low memory conditions iOS UIKit framework will send few notifications to our app,
- Implement the
applicationDidReceiveMemoryWarning:method of your application delegate.
- Override the
didReceiveMemoryWarningmethod in your custom
- Register to receive the
Upon receiving any of these notifications, your handler method should respond by immediately removing strong references to objects.
View controllers automatically remove references to views that are currently offscreen, but you should also override the didReceiveMemoryWarning method and use it to remove any additional references that your view controller does not need.
9) Use Instruments and Static Analyzer Xcode tools to check your app specific memory stats, and take further actions w.r.t. memory allocations, memory pressure and memory leaks.
For more information regarding this topic refer Apple’s official documentation here, https://developer.apple.com/library/ios/documentation/Performance/Conceptual/ManagingMemory/Articles/MemoryAlloc.html and https://developer.apple.com/library/mac/documentation/developertools/conceptual/instrumentsuserguide/MemoryManagementforYouriOSApp/MemoryManagementforYouriOSApp.html#//apple_ref/doc/uid/TP40004652-CH11-SW1
Hope this post was helpful, any comments or suggestions are acceptable.