UIImage memory not deallocating VM: ImageIO_JPEG_D

2020-06-13 05:37发布

I have multiple collection views on screen at one time that scroll horizontally. They are all filled with images. All of these images are being loaded in the background through the Parse api. I am running Instrument's allocations and the anonymous VM: ImageIO_JPEG_DATA category is taking up a majority of the memory being used. All memory in the app equals to about 40 and then this category is over 55, which puts the total right around 100. That category never goes down at all and just stays consistent. What can I do to free up this memory from the images in my collection views?

Here is the code for my collection view:

.m for my collection view controller

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];

    if (cell) {
        [cell.loadingImageIndicator setHidden:NO];
        [cell.loadingImageIndicator startAnimating];

        id photo = [self.collectionViewPhotos objectAtIndex:indexPath.item];

        if ([photo isKindOfClass:[PFObject class]]) {
            PFObject *photoObject = (PFObject *)photo;

            PFFile *imageFile = [photoObject objectForKey:kPhotoPictureKey];
            [cell.cellImage setFile:imageFile];

            [cell.cellImage loadInBackground:^(UIImage *image, NSError *error) {
                [cell.cellImage setContentMode:UIViewContentModeScaleAspectFill];
                [cell.loadingImageIndicator stopAnimating];
                [cell.loadingImageIndicator setHidden:YES];
            }];
        } else if ([photo isKindOfClass:[UIImage class]]) {
            [cell.cellImage setImage:photo];
            [cell.cellImage setContentMode:UIViewContentModeScaleAspectFill];
        }
    }

    return cell;
}

.m for CollectionViewCell

- (void)prepareForReuse
{
    self.cellImage.image = nil;
}

- (void)dealloc
{
    self.cellImage = nil;
}

Edit: Photo of instruments enter image description here

1条回答
We Are One
2楼-- · 2020-06-13 06:33

I had the same issue in a photo gallery-type app, and ran into the same problem with allocations in the so-called ImageIO_JPEG_DATA category accumulating and remaining "live" forever, causing my app to run out of memory. Oddly, this happenned only on the iPad I was testing on and NOT on the ios simulator (which showed no memory problems).

Brian's suggestion (below) worked for me. My app was originally using an array, each element of which contained - amongst other things - a UIImage. The images were used in various UIScrollViewControllers.

When I needed to load an image, if I used

[UIImage imageWithContentsOfFile:path]

rather than a direct reference to the UIImage in my array, the memory problem (caused by some inexplicable caching that ImageIO_Malloc was doing) was gone and the ImageIO_JPEG_DATA allocations stopped piling up and got released.

I would never have come up with this solution in a gazillion years on my own, so thanks to Brian.

查看更多
登录 后发表回答