I have an NSMutableArray of 15 elements, all of them strings. The strings refer to an image name that is stored locally in the app (images approximately 640x640 each). I am displaying the array of images in a UICollectionView.
When I reload the collectionView to display the images, my memory usage shoots up as you would expect displaying large png's (though its footprint is much larger than I expected).
The real problem is that memory used by these images is never freed. So despite me removing all objects from the array, removing the collectionView and setting everything to nil, none of the memory is freed.
Why is this? I would expect my memory to return to it's original level if I removed these objects and removed the ViewController.
UPDATED: Code snippet
ViewController.m
@implementation ViewController
static NSString *CellIdentifier = @"photoCell";
-(void)viewDidLoad{
[super viewDidLoad];
self.photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 320) collectionViewLayout:self.photoSpringFlowLayout];
[self.photoCollectionView setDataSource:self];
[self.photoCollectionView setDelegate:self];
[self.view addSubview:self.photoCollectionView];
[self.photoCollectionView registerClass:[PhotoPreviewCell class] forCellWithReuseIdentifier:CellIdentifier];
self.imagesAray = [[NSMutableArray alloc] initWithObjects:@"photo1", @"photo2", @"photo3", @"photo4", @"photo5", @"photo6", @"photo7", @"photo8", @"photo9", @"photo10", @"photo11", @"photo12", @"photo13", @"photo14", @"photo15", nil];
[self.photoCollectionView reloadData];
}
#pragma mark - UICollectionView Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.imagesArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PhotoPreviewCell *cell = (PhotoPreviewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *imageName = self.imagesArray[indexPath.row];
[cell.photoImage setImage:[UIImage imageNamed:imageName]];
return cell;
}
PhotoPreviewCell.m
@implementation PhotoPreviewCell
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
self.photoImage = [[UIImageView alloc] init];
self.photoImage.frame = CGRectMake(0, 0, 160, 160);
self.photoImage.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:self.photoImage];
}
return self;
}
Ok so the reason for this is that loading images from flash takes some time and OS tries to avoid loading it on and on, so it's caching loaded images. They're cached only when you're using imageNamed: method. If you're testing on simulator release all objects and try simulate memory warning. This should force OS to remove not used images from cache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With