IOS-let's talk about the memory issue in the ARC. why can't the array and object memory be released in the ARC ?, Ios-arc

Source: Internet
Author: User

IOS-let's talk about the memory issue in the ARC. why can't the array and object memory be released in the ARC ?, Ios-arc
1. preface I thought that after changing to ARC, I no longer need to consider the memory issue. However, in practice, I still find some memory problems that need to be noted. Today I will not talk about the block loop reference issue, this article mainly introduces some situations where objects and arrays cannot be released without memory. 2. array Memory cannot be released

// Organize dictionary data-(NSMutableDictionary *) setupDicData {NSMutableDictionary * dict = [NSMutableDictionary dictionary]; for (int I = 0; I <= 30; I ++) {[dict setObject: [self setupArrayData] forKey: [NSString stringWithFormat: @ "% d % @", I, @ "class"];} return dict ;} // organize the array data-(NSMutableArray *) setupArrayData {NSMutableArray * marry = [NSMutableArray array]; for (int I = 0; I <= 30; I ++) {NSString * s = [NSString stringWithFormat: @ "% @", @ "data-test"]; [marry addObject: s];} return marry ;}

Run + --

 

-(Void) viewDidLoad {[super viewDidLoad]; while (true) {// 30.0 scheduled execution [NSThread sleepForTimeInterval: 30.0]; NSDictionary * dict = [self setupDicData]; NSLog (@ "% @", dict); // the data memory is not released each time }}

 

// Execute the code by passing the array. the array and object memory are not released each time. The memory will increase wirelessly until it crashes. 2. Why does the memory not be released? When you use the while (true) {} wireless loop in iOS, By default, iOS ARC considers that this method has never been executed, so it does not take the initiative to release the objects in your method. This is different from JAVA, Therefore, many JAVA developers tend to use while (true) {} after switching to iOS ){} This memory risk exists in the project, resulting in an infinite increase in memory.

 

3. How can this problem be solved when the memory passed by the array cannot be released? Solution 1:3. 1. The simplest and most direct use of @ autoreleasepool {} in the ARC environment {}
// @ Autoreleasepool {} actively releases the memory once every cycle-(void) viewDidLoad {[super viewDidLoad]; while (true) {@ autoreleasepool {// 30.0 scheduled execution [NSThread sleepForTimeInterval: 30.0]; NSDictionary * dict = [self setupDicData]; NSLog (@ "% @", dict ); // The data memory is not released each time }}}

 

Memory graph. We found that the memory is stable and will be released automatically each time. Solution 2:. Use NSTimer for an infinite loop of array transmission. ARC will automatically release the memory for you.
-(Void) usingDatadosomething {// 30.0 scheduled execution [NSThread sleepForTimeInterval: 0.10]; NSDictionary * dict = [self setupDicData]; NSLog (@ "% @", dict ); // No data memory is released every time}-(void) viewDidLoad {[super viewDidLoad]; [nst1_scheduledtimerwithtimeinterval: 30.0 target: self selector: @ selector (usingDatadosomething) userInfo: self repeats: YES]; [[nsunloop currentRunLoop] run];}

The memory diagram is as follows:

 

Solution 3:3. 3. use block to encapsulate array transfer, and finally release the block. ARC will automatically help you release the memory block more frequently and will write a block blog next time. Thank you. If you have any questions, please comment on them. I have time to reply! Author: Clear Saup
Source: http://www.cnblogs.com/qingche/
The copyright of this article is shared by the author and the blog. You are welcome to repost it, but you must keep this statement and provide a connection to the original article on the article page.

 

 

 

 

 

 

 

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.