IOS-memory management is so simple 2

Source: Internet
Author: User

IOS-memory management is so simple 2

Civilization and comments are respect for yourself and encouragement to scholars. Thank you.

IOS-memory management is so simple 2
The main content discussed today is copy and mutableCopy.
1. Use copy and mutableCopy 1.
 NSArray *arr = [[NSArray alloc]initWithObjects:@"hello", nil];    NSArray *arr2 = [arr copy];        NSObject *obj = [[NSObject alloc]init];        NSLog(@"arr retainCout %ld",[arr retainCount]);    NSLog(@"arr2 retainCout %ld",[arr2 retainCount]);        NSLog(@"arr = %ld   arr2 =%ld ",arr,arr2);


Running result:

23:26:04. 977 MemoryManager [907: 40309] arr retainCout 2

23:26:04. 977 MemoryManager [907: 40309] arr2 retainCout 2

23:26:04. 977 MemoryManager [907: 40309] arr = 140677793151936 arr2 = 140677793151936

The same object is obtained through copy. copy adds the reference count of heap memory objects.









2.
NSArray *arr = [[NSArray alloc]initWithObjects:@"hello", nil];    NSArray *arr2 = [arr mutableCopy];        NSObject *obj = [[NSObject alloc]init];        NSLog(@"arr retainCout %ld",[arr retainCount]);    NSLog(@"arr2 retainCout %ld",[arr2 retainCount]);        NSLog(@"arr = %ld   arr2 =%ld ",arr,arr2);

Running result:

23:28:12. 817 MemoryManager [927: 41217] arr retainCout 1

23:28:12. 817 MemoryManager [927: 41217] arr2 retainCout 1

23:28:12. 817 MemoryManager [927: 41217] arr = 140285358613872 arr2 = 140285358605248

MutableCopy fully assigns a value to the content in arr and stores it in the newly allocated heap memory object. Therefore, arr and arr2 point to different memory objects;



NSArray *arr = [[NSArray alloc]initWithObjects:@"hello", nil];    NSArray *arr2 = [arr mutableCopy];    if([arr2 respondsToSelector:@selector(addObject:)]){        NSLog(@"changed");    }    NSObject *obj = [[NSObject alloc]init];        NSLog(@"arr retainCout %ld",[arr retainCount]);    NSLog(@"arr2 retainCout %ld",[arr2 retainCount]);        NSLog(@"arr = %ld   arr2 =%ld ",arr,arr2);

Running result:

23:32:03. 639 MemoryManager [950: 42492] changed

23:32:03. 640 MemoryManager [950: 42492] arr retainCout 1

23:32:03. 640 MemoryManager [950: 42492] arr2 retainCout 1

23:32:03. 640 MemoryManager [950: 42492] arr = 140367229411264 arr2 = 140367229427648

The mutableCopy function is equivalent,

NSMutableArray * arr2 = [[NSMutableArrayalloc] initWithArray: arr];








3.

NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello", nil];    NSMutableArray *arr2 = [arr mutableCopy];        if([arr2 respondsToSelector:@selector(addObject:)]){        NSLog(@"changed");    }    NSObject *obj = [[NSObject alloc]init];        NSLog(@"arr retainCout %ld",[arr retainCount]);    NSLog(@"arr2 retainCout %ld",[arr2 retainCount]);        NSLog(@"arr = %ld   arr2 =%ld ",arr,arr2);
Running result:


23:38:24. 378 MemoryManager [1034: 44814] changed

23:38:24. 379 MemoryManager [1034: 44814] arr retainCout 1

23:38:24. 379 MemoryManager [1034: 44814] arr2 retainCout 1

23:38:24. 379 MemoryManager [1034: 44814] arr = 140316907672672 arr2 = 140316907807120

Result Analysis: The variable array mutableCopy or variable array is equivalent

NSMutableArray * arr2 = [[NSMutableArray alloc] initWithArray: arr];

Arr and arr2 still point to objects in different stacks, but their contents are identical.


Note: After mutableCopy of a variable array, it is still variable.


5.

NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"hello", nil];    NSMutableArray *arr2 = [arr copy];        if([arr2 respondsToSelector:@selector(addObject:)]){        NSLog(@"changed");    }    NSObject *obj = [[NSObject alloc]init];        NSLog(@"arr retainCout %ld",[arr retainCount]);    NSLog(@"arr2 retainCout %ld",[arr2 retainCount]);        NSLog(@"arr = %ld   arr2 =%ld ",arr,arr2);


Running result:

23:41:36. 505 MemoryManager [1058: 46071] arr retainCout 1

23:41:36. 506 MemoryManager [1058: 46071] arr2 retainCout 1

23:41:36. 506 MemoryManager [1058: 46071] arr = 140719784280096 arr2 = 140719784275712

The variable array is equivalent to the following using the copy function:

NSArray * arr2 = [[NSArray alloc] initWithArray: arr];

Different heap objects share the same content, which is called Deep copy and shallow copy. But pay attention to the changes in the attributes of the copied objects, other collection classes and strings are also changed through deep copy and shortest copy, so be careful when using the application.

Note: The variable array copy becomes immutable.
II. loop references and weak references solve the memory leakage of loop references: the first thing to note is that only when the application count is 0 Can the memory objects on the stack call fictitious functions in the C ++ language to clean up the memory. If both objects hold strong references from each other, in this way, a reference ring will always exist in the memory, which will cause memory leakage.
Assumeretain class:
@interface CycleRetain : NSObject@property (nonatomic,retain)id retainObject;@end@implementation CycleRetain@end




Assumeretain * obj1 = [[assumeretain alloc] init]; // The reference count of the heap object to which obj1 points is 1 CycleRetain * obj2 = [[assumeretain alloc] init]; // The reference count of the heap object to which obj2 points is 1 NSLog (@ "before"); NSLog (@ "obj1 retainCount = % ld obj2 retainCount = % ld", obj1.retainCount, obj2.retainCount); obj1.retainObject = obj2; // because retainObject is a retain attribute, add 1 to the reference count of the heap memory object pointed to by obj2. In this case, it is 2 obj2.retainObject = obj1; // because retainObject is a retain attribute, the reference count of the heap memory object to which obj1 points must be added with 1, which is 2 NSLog (@ "after "); NSLog (@ "obj1 retainCount = % ld obj2 retainCount = % ld", obj1.retainCount, obj2.retainCount); // return the result when the function execution ends, at this time, the obj1 and obj2 declaration cycles will end. // the memory management principle and the scope of the creation will be released. [obj1 release]; // The application count of the heap memory object pointed to by obj1 minus 1. The reference count is 1. The reference count owner is the retainOject attribute [obj2 release] of obj2. // The application count of the heap memory object pointed to by obj2 is reduced by 1. The reference count is 1, and the reference count owner is the retainOject attribute of obj1. // This function is complete, there are still two referenced objects in the heap memory. No pointer variables on the stack reference them and they cannot be obtained, so they cannot be released, so as to find out the memory leakage.


Running result:

00:12:13. 260 MemoryManager [1186: 56593] before

00:12:13. 260 MemoryManager [1186: 56593] obj1 retainCount = 1 obj2 retainCount = 1

00:12:13. 261 MemoryManager [1186: 56593] after

00:12:13. 261 MemoryManager [1186: 56593] obj1 retainCount = 2 obj2 retainCount = 2








If you want to solve the problem, you can set one of them as a weak reference. Therefore, when designing a program, pay attention to the memory leakage caused by cyclic references, use the assign attribute appropriately to avoid circular reference. When using block,

Block_copy (<#... #>) It will also cause loop applications. If some external block variables are used in the block, you need to modify weak references through _ weak to avoid circular references.

For example:

__weak ViewController *weakVc = self;    dispatch_async(dispatch_get_main_queue(), ^{        weakVc.view.backgroundColor =[UIColor redColor];    });

Ii. Meanings of attributes modified by copy, retain, strong, and assign: Attributes modified by assign:
#import 
 
  @interface CycleRetain : NSObject@property (nonatomic,assign)id retainObject;@end
 

Assumeretain * obj1 = [[assumeretain alloc] init]; // The reference count of the heap object to which obj1 points is 1 CycleRetain * obj2 = [[assumeretain alloc] init]; // The reference count of the heap object to which obj2 points is 1 NSLog (@ "before"); NSLog (@ "obj1 retainCount = % ld obj2 retainCount = % ld", obj1.retainCount, obj2.retainCount); obj1.retainObject = obj2; // The reference count of the heap memory object pointed to by obj2 is 1 obj2.retainObject = obj1; // The reference count of the heap memory object pointed to by obj1 is 1 NSLog (@ "after") Because retainObject is an assign attribute "); NSLog (@ "obj1 retainCount = % ld obj2 retainCount = % ld", obj1.retainCount, obj2.retainCount );


Running result:

MemoryManager [633: 14446] obj1 retainCount = 1 obj2 retainCount = 1


The effects of strong and retain attributes are the same:
The type of the property object modified by copy must implement the NSCopying protocol. Otherwise, an error occurs,

Conclusion: memory management refers to the destruction of stack memory variables and the destruction of heap memory objects as long as you remember that you want to have it, retain, or copy. When your lifecycle ends, release, if you are not an absolute owner, use assign, but you cannot release. therefore, in programming, expressions are decomposed into Stack variable analysis, which determines that the life cycle of heap memory variables is the essence of OC memory management. The retain, copy, assign, and release variables on the stack determine whether the reference count of the variables in the stack is increased or decreased, thus determining the survival of the heap memory variables.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.