Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strong and Weak Confusion in iOS

I am little confused about using Strong or Weak in my particular case.

I have one class ParentClass which has 3 object ContainerClass1, ContainerClass2 and ContainerClass3.

Each ContainerClass has its own strong properties with Mutable objects like NSMutableArray

Now in my case, I have to show only one ContainerClass at a time, so if ContainerClass1 is shown then ContainerClass2 and ContainerClass3 is not required.

So I thought when I show ContainerClass1, will set ContainerClass2 and ContainerClass3 objects to nil. Here I am confused whether just setting the other ContainerClass(not shown) to nil will release its memory? because they have strong properties to other objects.

Or should I need to set all other ContainerClass's strong properties to nil first and then set ContainerClass to nil?

Thanks in advance.

like image 736
Zoeb S Avatar asked Apr 16 '15 05:04

Zoeb S


2 Answers

@zoeb, may this link will help you to keep away from basic memory problems.

how-to-overcome-memory-problems-in-iphone-applications-with-automatic-reference-counting

Edited:

As we know that Apple introduced ARC in IOS 5.0, ARC is compiler level feature that simplifies process of lifetime of objective-c objects. Before ARC introduced, We managed memory manually means “Manual Reference Counting(MRC)” . With MRC, Developer need to remember when to release or retain object. Means that Developer need to manage life cycle of objective-c objects.

According to Developer perspective, We are mostly interested to adding new features in our application rather then focusing on memory issues. But the things is sure that memory management perform crucial role in application success. To Provide help to Developer, Apple was figure out the way of automatically manage memory.

ARC is smartly manage memory but this is not 100 percent. We need to focus on some points while development to remove our application from lack of memory problem. Here i will try to provide solution of manage memory in ARC base application. that is not 100 percent as well. but its will try to help compiler to estimate life cycle of objective object.

Here are the some steps that you need to implement in your every controllers.

Step 1. Declare weak property to every UI Controls that used in application.

Example :
@property (nonatomic, weak) IBOutlet UIButton* btnPost;

@property (nonatomic, weak) IBOutlet UITableView* tblMessages;

etc.

Step 2. As per our developer most confusing question is that whether compiler allow to declare “dealloc” method in ARC base application. the answer is yes but don’t allowed to declare “[super dealloc]” inside it. so override “dealloc” method in every controllers.

-(void)dealloc{

}

Step 3. Remove heavy loaded object from superview in “dealloc” method rather then setting just “nil” reference like MKMapview, ScrollView etc.

-(void)dealloc{
dictAddress = nil;
arrayList = nil;
[map removeFromSuperview];
[scrollView removeFromSuperview];
}

Step 4. Avoid dead lock mechanism. (Example : Class A and Class B is there. Class B is declared Delegate with property type “Strong”. so that Class A and Class B dependent on each other on one is going to release. so in that case “dealloc” method is not called of either classes. so that class keep in memory. to removed such cases we need to keep “Assign” reference to Delegate object.) this is just for example. We need to consider other things as well like “Keep weak reference for block so it will release objects once its execution completed”.

these are the basic steps that avoiding memory problems. Still if you face memory problems then you need to take help of Analyzer to find leak and memory usage.

Below link will help you to analyze memory.

Mamory analyzer

like image 70
Jatin Patel - JP Avatar answered Oct 10 '22 14:10

Jatin Patel - JP


The confusion between strong and weak will be clear with the below link.

Differences between strong and weak in Objective-C

like image 1
Gaurav Gilani Avatar answered Oct 10 '22 14:10

Gaurav Gilani