ios-again on the arc memory problem, arc array, object memory is not released?

Source: Internet
Author: User
Tags finally block

1. PrefaceOriginally thought that after the conversion to arc, no longer need to consider the memory problem, but in practice still found some memory problems need to pay attention to, today I do not talk about block circular reference problem, mainly say some objects, arrays not memory is not released in the case.2. An array of memory is not released
Organization Dictionary data-(nsmutabledictionary *) setupdicdata{       nsmutabledictionary *dict = [nsmutabledictionary dictionary];    for (int i = 0; I <=; i++) {               [dict setobject:[self setuparraydata] forkey:[nsstring stringwithformat:@ "%d%@", i,@ "Class"];    }    return dict;} Organization 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 timed execution               [nsthread sleepfortimeinterval:30.0];               Nsdictionary *dict = [self setupdicdata];               NSLog (@ "%@", dict);        The data memory is not freed each time    }}

The array execution is passed on by code, and each array, object memory is not freed. The memory increases wirelessly until it crashes.2. What causes this kind of memory not to be released? mostly when you use the while (TRUE) {} wireless loop in iOS, IOS arc defaults to think that you're never done with this method, so you won't be able to voluntarily release objects in your method, which is not the same as Java, So many Java developers are accustomed to using while (true) {} After turning to iOS causes this memory hidden danger in the project, causes the memory to increase indefinitely.

3. How can I resolve this array of memory not being released? Workaround One:3.1. Simplest and most straightforward to use in arc environments @autoreleasepool {}
The function of the @autoreleasepool {} is to release the memory voluntarily at each cycle  -(void) viewdidload {        [super viewdidload];            while (true) {               @autoreleasepool {            //30.0 timed execution                        [nsthread sleepfortimeinterval:30.0];                        Nsdictionary *dict = [self setupdicdata];                        NSLog (@ "%@", dict);            The data memory is not released every time        }    

Memory graph, we found very stable, each time will actively release memory Workaround Two:3.2. Use Nstimer to do an infinite loop of array passing, and arc will automatically release the memory for you
-(void) usingdatadosomething{       //30.0 timed Execution       [nsthread sleepfortimeinterval:0.10];       Nsdictionary *dict = [self setupdicdata];       NSLog (@ "%@", dict);    Each data memory is not released}-(void) viewdidload {       [super viewdidload];       [Nstimer scheduledtimerwithtimeinterval:30.0 target:self selector: @selector (usingdatadosomething) userinfo:self Repeats:yes];       [[Nsrunloop Currentrunloop] run];   }

The memory graph is as follows

Workaround Three:3.3. Use block encapsulation array to pass, finally block release, ARC will automatically help you free memoryBlock use more frequently, not in this article elaborated, next will write a block of blog. Thank you, what questions can be commented on the proposed, I have time to reply patiently!Clear Saup
Source:http://www.cnblogs.com/qingche/
This article is copyright to the author and the blog Park is shared, welcome reprint, but must retain this paragraph statement, and in the article page obvious location to give the original text connection.

ios-again on the arc memory problem, arc array, object memory is not released?

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.