I’m trying to load some data from a plist file. It contains an NSArray of NSDictionaries with very few data each (a very short string and a number). The problem is that it has about 8000 entries. The plist file itself is about 900 kB when stored as XML and about 350 kB when stored as binary, but apparently when it gets loaded into memory it expands to about 510 MB, that’s what seems to happen using the allocation analyser. That’s like 50-60k times the on-disk size.
At first I thought that there must have been some kind of error but then I realised I know very little of memory overhead of stuff in Cocoa. Anyway. I load the plist like this:
NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"localita" ofType:@"plist"]];
then I assign it to a property (nonatomic, strong) of this custom view controller I have
[cbVC setData:data];
[cbVC setFilteredData:data];
I was overriding the setData before but I changed to this way to see if that was a problem—it wasn’t. So, this is the interface:
@protocol CityBrowserDelegate <NSObject>
- (void)cityPicked:(NSString *)cityName;
@end
@interface CityBrowserViewController : UIViewController <UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope;
- (IBAction)selectionFinished:(id)sender;
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSMutableArray *filteredData;
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) IBOutlet id<MyPopoverControllerDelegate, CityBrowserDelegate> delegate;
@end
and this is the implementation (I stripped out non-relevant parts)
@implementation CityBrowserViewController
@synthesize data = _data;
@synthesize filteredData = _filteredData;
@synthesize tableView = _tableView;
@synthesize delegate = _delegate;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.filteredData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.filteredData count] == 0 ? 1 : [self.filteredData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"BasicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
if([self.filteredData count] == 0) {
[cell.textLabel setText:@"Nessun risultato"];
} else {
[cell.textLabel setText:[[self.filteredData objectAtIndex:indexPath.row] valueForKey:@"Name"]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *name = [[self.filteredData objectAtIndex:indexPath.row] valueForKey:@"Name"];
[self.delegate cityPicked:name];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
_filteredData = _data;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[_filteredData removeAllObjects]; // First clear the filtered array.
// YES, I KNOW, but it seems to be quite fast nonetheless...
NSDictionary *city;
for (city in _data) {
NSString *cityName = [city valueForKey:@"Name"];
NSComparisonResult result = [cityName compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame) {
[_filteredData addObject:city];
}
}
}
- (void)selectionFinished:(id)sender {
[self.delegate dismissPopover];
}
@end
Now: any clue about why I get 510 MB of memory allocated when I load that plist? And apparently it doesn’t get deallocated when I cancel the search and restore the original data in searchBarCancelButtonClicked…
The problem was that I was returning N sections and N rows per section, given N the size of the dataset. Clearly not the best idea. The dataset is only about 8000 entries big and it won’t change any time soon so for now I’ll leave the code as is (with the obvious correction). In a separate branch I’m experimenting with Core Data.