Question

I'm new to iOS development and in my app I'm seeing some strange memory usage behavior.

I'm getting objects from server in such setupDataForPage method:

- (void)setupDataForPage:(int)page actionType:(NSString *)type success:(void (^)())callback 
{
__weak MyTableViewController *weakSelf = self;

// clearing image cache because feed contains a lot of images
[[SDImageCache sharedImageCache] clearMemory];
[[SDImageCache sharedImageCache] clearDisk];

MyHTTPClient *API = [MyHTTPClient new];

[API feedFor:page success:^(AFHTTPRequestOperation *operation, id data) {
    NSArray *data = [data objectForKey:@"data"];

    if ([data count] > 0) {
        // remove all objects to refresh with new ones
        if ([type isEqualToString:@"pullToRefresh"]) {
            [weakSelf.models removeAllObjects];
        }

        // populate data
        NSMutableArray *result = [NSMutableArray new];
        for (NSDictionary *modelData in data) {
            MyModel *model = [[MyModel alloc] initWithDictionary:modelData];
            [result addObject:model];
        }

        [weakSelf.models addObjectsFromArray:result];
        [weakSelf.tableView reloadData];
    }

    callback();
} failure:nil];
}

it is used in viewDidLoad while getting initial request and also for pull refresh and infinite scrolling:

- (void)viewDidLoad {
[super viewDidLoad];

__block int page = 1;
__weak MyTableViewController *weakSelf = self;

// initial load
[self setupDataForPage:page actionType:@"initial" success:^{ page += 1; }];

// pull to refresh
[self.tableView addPullToRefreshWithActionHandler:^{
    [weakSelf setupDataForPage:1 actionType:@"pullToRefresh" success:^{
        [weakSelf.tableView.pullToRefreshView stopAnimating];
    }];
}];

// infinite scrolling
[self.tableView addInfiniteScrollingWithActionHandler:^{
    [weakSelf setupItemsForPage:page actionType:@"infiniteScroll" success:^{
        page += 1;
        [weakSelf.tableView.infiniteScrollingView stopAnimating];
    }];
}];
}

I noticed that even after pull to refresh action which returns the same data (and I'm just removing all models and add them once more) my app's memory usage grows from nearly 19mb to 24mb..

I would like someone more experienced to look at this piece of code to determine whether it contains some possible memory leaks.. Should I somehow delete NSMutableArray *result variable after assigning it to models array?

Thanks!

Was it helpful?

Solution

First of all, use @autoreleasepool here:

@autoreleasepool {
    NSArray *data = [data objectForKey:@"data"];

    if ([data count] > 0) {
        // remove all objects to refresh with new ones
        if ([type isEqualToString:@"pullToRefresh"]) {
            [weakSelf.models removeAllObjects];
        }

        // populate data
        NSMutableArray *result = [NSMutableArray new];
        for (NSDictionary *modelData in data) {
            MyModel *model = [[MyModel alloc] initWithDictionary:modelData];
            [result addObject:model];
        }

        [weakSelf.models addObjectsFromArray:result];
        [weakSelf.tableView reloadData];
    }
}

@autoreleasepool allows you to release every object allocated in that scope IMMEDIATELY.

This is perfect situation where use it ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top