title: What's the memory overhead of a plist file with an array of dictionaries?
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"localita" ofType:@"plist"]];
  </code></pre>
  
  <p>then I assign it to a property (nonatomic, strong) of this custom view controller I have</p>
  
  <pre><code>[cbVC setData:data];
  [cbVC setFilteredData:data];
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>@protocol CityBrowserDelegate &lt;NSObject&gt;
  
  - (void)cityPicked:(NSString *)cityName;
  
  @end
  
  @interface CityBrowserViewController : UIViewController &lt;UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate&gt;
  
  - (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&lt;MyPopoverControllerDelegate, CityBrowserDelegate&gt; delegate;
  
  @end
  </code></pre>
  
  <p>and this is the implementation (I stripped out non-relevant parts)</p>
  
  <pre><code>@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
  </code></pre>
  
  <p>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 <code>searchBarCancelButtonClicked</code>…</p>